这道题和之前的把那一道1222很类似。仅仅只是一定要注意一下对于无解的推断。
/*#########################################################################
# File Name: poj_1681.cpp
# Author: CaoLei
# Created Time: 2015/7/20 18:33:08
#########################################################################*/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
using namespace std;
#define MAX(x,y) (((x)>(y))?(x):(y))
#define MIN(x,y) (((x)<(y))?(x):(y))
#define N 500010
#define pi acos(-1.0)
#define inf 100000000
typedef long long ll;
typedef unsigned long long ull;
int a[300][300];
char str[20];
int n;
bool f;
void gauss(){
for (int i = 0; i<n*n; i++){
int p=i;
for (int j = i; j<n*n; j++){ //这个地方j的初始值是i不
//是0,错了一次切记。
if (a[p][i]<a[j][i]){
p=j;
}
}
for (int j = 0; j <= n*n; j++) swap(a[i][j], a[p][j]);
int count=0;
for(int k=i;k<n*n;k++) //推断是否有解,关键
if(a[i][k]) count++;
if(!count&&a[i][n*n]){
f=false;
return ;
}
for (int j = 0; j<n*n; j++)
if (i != j&&a[j][i])
for (int k = i + 1; k <= n*n; k++)
a[j][k] ^= a[i][k];
}
}
int main(){
freopen("in.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--){
f = true;
memset(a, 0, sizeof(a));
scanf("%d", &n);
for (int i = 0; i<n; i++){
scanf("%s",str);
for (int j = 0; j<n; j++){
if (str[j] == 'w'){
a[i*n + j][n*n] = 1;
}
}
}
for (int j = 0; j<n*n; j++){
a[j][j] = 1;
if (j%n != 0) a[j - 1][j] = 1;
if ((j + 1) % n != 0) a[j + 1][j] = 1;
if (j >= n) a[j - n][j] = 1;
if (j<(n - 1)*n) a[j + n][j] = 1;
}
gauss();
if (!f){
printf("inf\n");
continue;
}
int count = 0;
for (int i = 0; i<n*n; i++){
if(a[i][n*n]) count++;
}
printf("%d\n",count);
}
return 0;
}