Ultraviolet A 11464 Even Parity (unique concept)

Source: Internet
Author: User

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){&nbsp;&nbsp; &nbsp;int sum=0;&nbsp;&nbsp; &nbsp;for(int i=0;i<3;++i) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;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]];&nbsp;&nbsp; &nbsp;return sum&1;}bool generate(int x,int &sum)//x>0{&nbsp;&nbsp; &nbsp;for(int i=0;i<n;++i)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(f(x-1,i)) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t[x][i]=1;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(!ori[x][i]) ++sum;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(ori[x][i]) {sum=INF;return 0;}&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t[x][i]=0;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;return 1;}int solve(int pre){&nbsp;&nbsp; &nbsp;int sum=pre;&nbsp;&nbsp; &nbsp;for(int i=1;i<n;++i) if(!generate(i,sum)) break;&nbsp;&nbsp; &nbsp;return sum;}void dfs(int pos,int step){&nbsp;&nbsp; &nbsp;if(pos>=n) {ans=min(ans,solve(step));return;}&nbsp;&nbsp; &nbsp;if(ori[0][pos]) {t[0][pos]=1;dfs(pos+1,step);}&nbsp;&nbsp; &nbsp;else &nbsp;&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t[0][pos]=0; dfs(pos+1,step);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t[0][pos]=1; dfs(pos+1,step+1);&nbsp;&nbsp; &nbsp;}}int main(){&nbsp;&nbsp; &nbsp;scanf("%d",&T);&nbsp;&nbsp; &nbsp;while(T--)&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;scanf("%d",&n);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(int i=0;i<n;++i)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(int j=0;j<n;++j) scanf("%d",&ori[i][j]);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ans=INF;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;dfs(0,0);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("Case %d: %d\n",++ca,ans>=INF? -1:ans);&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.