Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4839   Accepted: 2350

Description

There is a square wall which is made of n*n small square bricks. Some bricks are white while some bricks are yellow. Bob is a painter and he wants to paint all the bricks yellow. But there is something wrong with Bob's brush. Once he uses this brush to paint brick (i, j), the bricks at (i, j), (i-1, j), (i+1, j), (i, j-1) and (i, j+1) all change their color. Your task is to find the minimum number of bricks Bob should paint in order to make all the bricks yellow.

Input

The
first line contains a single integer t (1 <= t <= 20) that
indicates the number of test cases. Then follow the t cases. Each test
case begins with a line contains an integer n (1 <= n <= 15),
representing the size of wall. The next n lines represent the original
wall. Each line contains n characters. The j-th character of the i-th
line figures out the color of brick at position (i, j). We use a 'w' to
express a white brick while a 'y' to express a yellow brick.

Output

For
each case, output a line contains the minimum number of bricks Bob
should paint. If Bob can't paint all the bricks yellow, print 'inf'.

Sample Input

2
3
yyy
yyy
yyy
5
wwwww
wwwww
wwwww
wwwww
wwwww

Sample Output

0
15

Source

/**
题意:根据给出的图,问有多少种方法使得变为全‘y’
做法:高斯消元 建一个n*n的矩阵
**/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#define maxn 250
using namespace std;
int mmap[maxn][maxn];
int x[maxn];
int equ,val;
char ch[][];
int free_x[maxn];
int gcd(int a,int b)
{
if(b == ) return a;
return gcd(b,a%b);
}
int Lcm(int a,int b)
{
return a/gcd(a,b)*b;
}
int Guess()
{
int lcm;
int ta;
int tb;
int max_r;
int k;
int col;
col = ;
for(k = ; k<equ&&col < val; k++,col++)
{
max_r = k;
for(int i=k+; i<equ; i++)
{
if(abs(mmap[i][col]) > abs(mmap[max_r][col]))
{
max_r = i;
}
}
if(mmap[max_r][col] == )
{
k--;
continue;
}
if(max_r != k)
{
for(int i=col; i<val+; i++)
{
swap(mmap[max_r][i],mmap[k][i]);
}
}
for(int i=k+; i<equ; i++)
{
if(mmap[i][col] != )
{
for(int j=col; j<val+; j++)
{
mmap[i][j] ^= mmap[k][j];
}
}
}
}
for(int i=k; i<equ; i++)
{
if(mmap[i][col] != ) return -;
}
for(int i=val-; i>=; i--)
{
x[i] = mmap[i][val];
for(int j=i+; j<val; j++)
{
x[i] ^= (mmap[i][j] & x[j]);
}
}
return ;
}
void init(int n)
{
memset(x,,sizeof(x));
memset(mmap,,sizeof(mmap));
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
int tt = i * n +j;
mmap[tt][tt] = ;
if(i > ) mmap[(i-)*n+j][tt] = ;
if(i < n-) mmap[(i+)*n+j][tt] = ;
if(j > ) mmap[i*n + j - ][tt] = ;
if(j < n-) mmap[i*n + j + ][tt] = ;
}
}
}
void solve(int tt)
{
int res = Guess();
if(res == -) printf("inf\n");
else if(res == )
{
int ans = ;
for(int i=; i<=tt; i++)
{
ans += x[i];
}
printf("%d\n",ans);
return;
}
else
{
int ans = 0x3f3f3f3f;
int tot = (<<res);
for(int i=; i<tot; i++)
{
int cnt = ;
for(int j=; j<res; j++)
{
if(i &(<<j))
{
x[free_x[j]] = ;
cnt++;
}
else x[free_x[j]] = ;
}
for(int j=val-tt-; j>=; j--)
{
int k;
for( k=j; k<val; k++)
if(mmap[j][k]) break;
x[k] = mmap[j][val];
for(int l=k+; l < val; l++)
if(mmap[j][l]) x[k] ^= x[l];
cnt += x[k]; }
ans = min(ans,cnt);
}
printf("%d\n",ans);
}
return;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
char c[];
init(n);
int tt = n*n;
equ = val = tt;
for(int i=; i<n; i++)
{
scanf("%s",c);
for(int j=; j<n; j++)
{
if(c[j] == 'y') mmap[i*n+j][tt] = ;
else mmap[i*n+j][tt] = ; }
}
solve(tt);
}
return ;
}

最新文章

  1. Oracle Dataguard的原理与基本配置
  2. 【GoLang】GoLang 中 make 与 new的区别
  3. Caffe初试(一)win7_64bit+VS2013+Opencv2.4.10+CUDA6.5配置Caffe环境
  4. CentOS 6.5 源码编译搭建LNMP(三台独立主机实现)
  5. LintCode 整数排序
  6. useful-scripts
  7. Open-source Project官方地址
  8. ssh免密钥登录
  9. struts开发步骤
  10. RMI和socket详解
  11. Ubuntu系统下Anaconda使用方法总结
  12. GitHub上README.md编写教程(基本语法)
  13. 建立一个php 基础类
  14. POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)
  15. Verilog笔记.三段式状态机
  16. 如何计算 App 的启动时间
  17. Commons FileUpload文件上传组件
  18. Aria2 无限制下载神器
  19. Rendering Engine 主流的浏览器内核(排版引擎、渲染引擎、解释引擎)有哪几种,分别的特点
  20. Hadoop2的Yarn和MapReduce2相关

热门文章

  1. BZOJ1060:[ZJOI2007]时态同步——题解
  2. 【BZOJ 3569】DZY Loves Chinese II 随机化+线性基
  3. Linux之ioctl20160705
  4. noip模拟赛 保留道路
  5. 牛客328B Rabbit的工作(1)
  6. Leetcode 381. O(1) 时间插入、删除和获取随机元素 - 允许重复
  7. BI在连锁零售业应用
  8. 自定义函数,根据p个数,自适应剧中效果
  9. X210(s5pv210)中断系统
  10. POJ 3087 Shuffle&#39;m Up DFS