#include<iostream> #include<cstdio> #include<cstring> using namespace std; int map1[230][230], ans, n, m; int dir[4][2] = {0,1,0,-1,1,0,-1,0}; int gauss() { int i,j,k,t; for(i=0,j=0; i<n && j<n; i++,j++) { k=i; while(!map1[k][j] && k<n) k++; if(k==n) { i--; continue; } if(k!=i) for(t = j; t <= n; t++) swap(map1[i][t],map1[k][t]); for(k=i+1; k < n; k++) if(map1[k][j]) for(t=j; t<=n; t++) map1[k][t]^=map1[i][t]; } k=i; for(i=k; i<n; i++) { if(map1[i][n]) return -1; //while(1); } for(i=k-1,t=0; i >= 0; i--) { for(j=i+1; j<n; j++) map1[i][n]^=(map1[i][j]&&map1[j][n]); if(map1[i][n]) t++; } return t; } int main() { int cas, i, j, ans; char c; scanf("%d", &cas); while (cas--) { scanf("%d", &m); memset(map1, 0, sizeof (map1)); n = m*m; for (i = 0; getchar(), i < m; i++) for (j = 0; j < m; j++) { c = getchar(); if (c == ‘w‘) map1[i * m+j][n] = 1; else map1[i * m+j][n] = 0; } for (i = 0; i < m; i++) for (j = 0; j < m; j++) { map1[i*m+j][i*m+j]=1; for(int k=0; k<4; k++) { int dx=i+dir[k][0],dy=j+dir[k][1]; if(dx>=0 && dx<m && dy>=0 && dy<m) map1[dx*m+dy][i*m+j]=1; } } ans=gauss(); if(ans==-1) printf("inf\n"); else printf("%d\n",ans); } return 0; } /* 5 4 wwww wwww wwww wwww 5 result 4 but compute result is 10 . this is because free radical is not enum. */
时间: 2024-12-28 09:47:31