Description
There is a square wall which is made the nxn small square bricks. Some bricks are white while Some bricks are. Bob is a painter and him wants to paint 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 their color. Your task is to find the minimum number of bricks Bob should paint into order to make all the bricks yellow.
Input
The "a single Integer T" (1 <= t <=) 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 <=), 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
the
Give a nxn nxn small brick composition of the wall, now to paint its uniform yellow, but each painting a small brick will make the adjacent four to change to the opposite color, ask at least how many times to paint to be successful.
train of Thought
Considering the NxN nxn polynomial, each polynomial involves five variables of the top and bottom and its own, with a result of 1.
Then the solution of the equations is calculated by the Gauss elimination, and if the unique solution is given, the number of 1 is counted, and if there are infinitely many solutions, all the free arguments are enumerated to find the optimal solution, if no output inf is obtained.
AC Code
#include <cstdio> #include <cstdlib> #include <cstring> #include <stdlib.h> #include < iostream> #include <queue> #include <vector> #include <map> #include <cmath> #include <
Algorithm> using namespace std;
typedef __int64 LL;
const int MAXN = 300;
int Equ,var;
int A[MAXN][MAXN];
int X[MAXN];
int FREE_X[MAXN];
int free_num;
int n;
int Gauss () {int max_r,col,k;
free_num=0;
for (k=0,col=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 (a[max_r][col]==0) {k--;
Free_x[free_num++]=col;
Continue
} if (max_r!=k) for (int j=col; j<var+1; j + +) Swap (A[K][J],A[MAX_R][J);
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;
if (K<var) return var-k;
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 Init () {memset (a,0,sizeof (a));
memset (x,0,sizeof (x));
Equ=n*n;
Var=n*n;
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[i*n+j-1][t]=1;
if (j<n-1) a[i*n+j+1][t]=1;
} void Solve () {int T=gauss ();
if (t==-1) cout<< "INF" <<endl;
else if (t==0) {int ans=0;
for (int i=0; i<n*n; i++) ans+=x[i];
cout<<ans<<endl;
else {int ans=0x3f3f3f3f;
int tot=1<<t; for (int i=0; i<tot; i++) {int cnt=0; for (int j=0; j<t; J + +) {if (i& (1<<j)) {X[fre
E_x[j]]=1;
cnt++;
else x[free_x[j]]=0;
for (int j=var-t-1; j>=0; j--) {int idx;
for (idx=j; idx<var; idx++) if (A[J][IDX)) a break;
X[idx]=a[j][var];
for (int l=idx+1; l<var; l++) if (a[j][l)) x[idx]^=x[l];
CNT+=X[IDX];
} ans=min (ANS,CNT);
} cout<<ans<<endl;
int main () {Ios::sync_with_stdio (false);
int T;
cin>>t;
while (t--) {char str[30];
cin>>n;
Init (); for (int i=0; i<n; i++) {CIn>>str;
for (int j=0; j<n; J + +) if (str[j]== ' y ') a[i*n+j][n*n]=0;
else a[i*n+j][n*n]=1;
} solve ();
return 0; }