Topic links
Key: And POJ1753FILP game, the difference is the T Group of data and flip the time of a more upper left corner.
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<queue>#include<vector>#include<map>#include<ctime>using namespacestd;Const intmaxn= -;//there is a equ equation, var variable element. The number of rows in the augmented matrix is equ and the number of columns is var+1, 0 to Var, respectivelyintEquvar;intA[MAXN][MAXN];//Augmented matrixintX[MAXN];//Solution SetintFREE_X[MAXN];//used to store free variable (multi-Solution enumeration free variable can be used)intFree_num;//number of free arguments//A return value of 1 means no solution, 0 is the only solution, otherwise the number of free arguments is returnedintGauss () {intmax_r,col,k; Free_num=0; for(k=0, col=0; k<equ&&col<var; k++,col++) {Max_r=K; for(inti=k+1; i<equ; i++) if(ABS (A[i][col]) >ABS (A[max_r][col]) Max_r=i; if(!A[max_r][col]) {k--; Free_x[free_num++]=Col; Continue; } if(max_r!=k) for(intJ=col; j<var+1; J + +) Swap (a[k][j],a[max_r][j]); for(inti=k+1; i<equ; i++) { if(A[i][col]) { for(intJ=col; j<var+1; J + +) A[i][j]^=A[k][j]; } } } for(intI=k; i<equ; i++) if(A[i][col])return-1; if(k<var)return var-K; for(intI=var-1; i>=0; i--) {X[i]=a[i][var]; for(intj=i+1; j<var; J + +) X[i]^= (a[i][j]&&X[j]); } return 0;}intN;voidinit () {memset (A,0,sizeof(a)); memset (x,0,sizeof(x)); Equ=n*N; var=n*N; for(intI=0; i<n; i++) for(intj=0; j<n; J + +) { intt=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(i>0&&j>0) a[(I-1) *n+j-1][t]=1; if(j>0) a[i*n+j-1][t]=1; if(j<n-1) a[i*n+j+1][t]=1; }}intsolve () {intt=Gauss (); if(t==-1) { return-1; } Else if(t==0) { intans=0; for(intI=0; i<n*n; i++) ans+=X[i]; returnans; } Else { //Enumerate free variable elements intans=0x3f3f3f3f; intTot= (1<<t); for(intI=0; i<tot; i++) { intCnt=0; for(intj=0; j<t; J + +) { if(i& (1<<J))//Note not &&{X[free_x[j]]=1; CNT++; } Elsex[free_x[j]]=0; } for(intj=var-t-1; j>=0; j--) { intidx; for(Idx=j; idx<var; idx++) if(A[j][idx]) Break; X[IDX]=a[j][var]; for(intl=idx+1; l<var; l++) if(A[j][l]) X[idx]^=X[l]; CNT+=X[idx]; } ans=min (ans,cnt); } returnans; }}Charstr[ -][ -];intMain () {intT; scanf ("%d",&t); GetChar (); while(t--) {n=4; for(intI=0; i<4; i++) scanf ("%s", Str[i]); Init (); for(inti =0; I <4; i++) for(intj =0; J <4; J + +) { if(Str[i][j] = ='b') a[i*4+j][ -] =0; Elsea[i*4+j][ -] =1; } intANS1 =solve (); Init (); for(inti =0; I <4; i++) for(intj =0; J <4; J + +) { if(Str[i][j] = ='b') a[i*4+j][ -] =1; Elsea[i*4+j][ -] =0; } intAns2 =solve (); if(ans1 = =-1&& Ans2 = =-1) printf ("impossible\n"); Elseprintf"%d\n", Min (ans1,ans2)); } return 0;}
Nefu 504 New Flip Game (Gaussian elimination)