https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 2459
http://7xjob4.com1.z0.glb.clouddn.com/6892b750367614576602612088b7c161
Test instructions: Changes the 01 matrix of a given n to an even matrix
IDEA: Enumeration of the first row of 01 cases, the introduction of other lines of the situation, to determine whether the compliance, the complexity of O (2^n*n^2);
#include <bits/stdc++.h>using namespacestd;intT;intN;inta[ -][ -];intb[ -][ -];intCheckints) { inti,j; intCnt=0; memset (b,0,sizeof(b)); for(i=1; i<=n;i++) { if(S & (1<< (I-1)) {b[1][i]=1; if(a[1][i]==0) CNT++; } if(a[1][i]==1&& b[1][i]==0) { return-1; } } for(i=2; i<=n;i++) { for(j=1; j<=n;j++) { intsum=0; if(I-2>=1) {sum+=b[i-2][j]; } if(J-1>=1) {sum+=b[i-1][j-1]; } if(j+1<=n) {sum+=b[i-1][j+1]; } if(sum%2==0) {B[i][j]=0; if(a[i][j]==1) return-1; } Else{B[i][j]=1; if(a[i][j]==0) CNT++; } } } for(j=1; j<=n;j++) { intsum=0; if(n1>=1) sum+=b[n-1][j]; if(J-1>=1) sum+=b[n][j-1]; if(j+1>=1) sum+=b[n][j+1]; if(sum%2==1) { return-1; } } returnCNT;}intMain () {inti,j; intcas=1; scanf ("%d",&T); while(t--) {scanf ("%d",&N); for(i=1; i<=n;i++) { for(j=1; j<=n;j++) {scanf ("%d",&A[i][j]); } } intans= -; for(ints=0;s< (1<<N); s++) { intf=check (s); if(f>=0) {ans=min (ans,f); } } if(ans== -) ans=-1; printf ("Case %d:%d\n", cas++, ans); } return 0;}
View Code
UVA 11464 even Matrix