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
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
15
1
2
 

题意:

和​​ POJ.1222 EXTENDED LIGHTS OUT ​​几乎一样,区别只是规定了维数,不再是5×6的矩阵,而是m×n的矩阵。并且直接输出需要修改的次数即可。 

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define nmax 305
using namespace std;
typedef long long ll;
int a[nmax][nmax];
int x[nmax];
int hashback[nmax][nmax];
int free_x[nmax];
char mp[nmax][nmax];
int ans1,ans2;
int equ,var,n;
int Gauss(){
int max_r;
int col=0,num = 0;
int k;
for(int i = 0;i<=var;++i) x[i] = free_x[i] = 0;
for(k = 0;k < equ && col < var;k++,col++){
max_r=k;
for(int i=k+1;i<equ;i++){
if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;
}
if(max_r!=k){
for(int j=k ;j<var+1;j++) swap(a[k][j],a[max_r][j]);
}
if(a[k][col]==0){
free_x[num++] = col;
k--; continue;
}
for(int i=k+1;i<equ;i++){
if(a[i][col]!=0){
for(int j=col;j<var+1;j++){
a[i][j]^=a[k][j];;
}
}
}
}
for(int i = k;i<equ;++i){
if(a[i][col] != 0) return -1;
}

for(int i = var - 1; i >= 0; i--){
x[i]=a[i][var];
for(int j = i + 1; j < var; j++){
x[i] ^= ( a[i][j] && x[j]);
}
}
return 0;
}
void enum_freex(int n,int & ans){
int num = (1<<(n));
ans = 1e9+7;
for(int i = 0;i<num;++i){
int cnt = 0;
for(int j = 0;j<n;++j){
if(i&(1<<j)){
cnt++;
x[free_x[j]] = 1;
}else x[free_x[j]] = 0;
}
for(int k = var-n-1;k>=0;--k){// 没有自由元的最下面一行
int index = 0;
for(index = k;k<var;index++){// 在当前行找到第一个非0自由元(如果存在的话)
if(a[k][index]) break;
}
x[index] = a[k][var];
for(int j = index+1;j<var;++j){// 向后依次计算出结果
if(a[k][j]) x[index] ^= x[j];
}
cnt += x[index]; // 如果结果为1,则统计
}
ans = min(ans,cnt);
}
}
void init()
{
memset(a, 0, sizeof(a));
memset(x, 0, sizeof(x));

for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
int t=i*n+j;
a[t][t]=1;
if(i>0)a[(i-1)*n+j][t]=1;
if(i<n-1) a[(i+1)*n+j][t]=1;
if(j>0) a[t-1][t]=1;
if(j<n-1) a[t+1][t]=1;
}

}
int main()
{

int T,cas=0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
equ=n*n;
var=n*n;

init();
for(int i = 0;i<n*n;++i)
{
char ch;
scanf(" %c",&ch);
if(ch =='y') a[i][n*n] = 0;
else a[i][n*n] = 1;
}
if(Gauss()==-1)
{
printf("inf\n");
continue;
}
ll ans=0;
for(int i = 0;i<n*n;++i)
{
//printf("%d\n",x[i]);
if(x[i] == 1) ans++;
}
printf("%d\n",ans);

}
return 0;
}