There is an n * n 01 matrix. The task is to change as few as 0 as possible to 1 so that the sum of the top, bottom, left, and right elements of each element is an even number.
Idea: it is easy to think of the idea that the enumeration of each vertex is 0 or 1, because n <= 15, the complexity is 2 ^ 225 obviously TLE. Note that after each confirmation is the same, the next line can be determined! Therefore, as long as the status of the first row is enumerated, the status of each row can be introduced. The complexity is 15*2 ^ 15.
#include<cstdio>#include<iostream>#define INF 0x3f3f3f3f#define MAXN 20using namespace std;int n,ori[MAXN][MAXN],t[MAXN][MAXN],dx[]={0,0,-1},dy[]={1,-1,0},ans,ca=0,T;bool f(int x,int y){ int sum=0; for(int i=0;i<3;++i) if(x+dx[i]>=0 && y+dy[i]>=0 && x+dx[i]<n && y+dy[i]<n) sum+=t[x+dx[i]][y+dy[i]]; return sum&1;}bool generate(int x,int &sum)//x>0{ for(int i=0;i<n;++i) if(f(x-1,i)) { t[x][i]=1; if(!ori[x][i]) ++sum; } else { if(ori[x][i]) {sum=INF;return 0;} t[x][i]=0; } return 1;}int solve(int pre){ int sum=pre; for(int i=1;i<n;++i) if(!generate(i,sum)) break; return sum;}void dfs(int pos,int step){ if(pos>=n) {ans=min(ans,solve(step));return;} if(ori[0][pos]) {t[0][pos]=1;dfs(pos+1,step);} else { t[0][pos]=0; dfs(pos+1,step); t[0][pos]=1; dfs(pos+1,step+1); }}int main(){ scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n;++i) for(int j=0;j<n;++j) scanf("%d",&ori[i][j]); ans=INF; dfs(0,0); printf("Case %d: %d\n",++ca,ans>=INF? -1:ans); } return 0;}