uva 11464 - Even Parity
2014-06-03 15:03
274 查看
We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a one(1).
The parity of a cell is the number of 1s surrounding that cell. A cell is surrounded by at most 4 cells (top, bottom, left, right).
Suppose we have a grid of size 4 x 4:
For this problem, you have to change some of the 0s to 1s so that the parity of every cell becomes even. We are interested in the minimum number of transformations of 0 to 1 that is needed to achieve the desired requirement.
Input
The first line of input is an integer T (T<30) that indicates the number of test cases. Each case starts with a positive integer N(1≤N≤15). Each of the next N lines contain N integers (0/1) each. The integers are separated by a single space character.
[align=center][/align]
The parity of a cell is the number of 1s surrounding that cell. A cell is surrounded by at most 4 cells (top, bottom, left, right).
Suppose we have a grid of size 4 x 4:
1 | 0 | 1 | 0 | The parity of each cell would be | 1 | 3 | 1 | 2 |
1 | 1 | 1 | 1 | 2 | 3 | 3 | 1 | |
0 | 1 | 0 | 0 | 2 | 1 | 2 | 1 | |
0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
Input
The first line of input is an integer T (T<30) that indicates the number of test cases. Each case starts with a positive integer N(1≤N≤15). Each of the next N lines contain N integers (0/1) each. The integers are separated by a single space character.
Output
For each case, output the case number followed by the minimum number of transformations required. If it's impossible to achieve the desired result, then output -1 instead.Sample Input Output for Sample Input
3 3 0 0 0 0 0 0 0 0 0 3 0 0 0 1 0 0 0 0 0 3 1 1 1 1 1 1 0 0 0 | Case 1: 0 Case 2: 3 Case 3: -1 |
/* 把尽量少的0变成1,使得每个元素上下左右的元素(存在的话)之和均为偶数 */ #include <iostream> #include <string> #include <cstdio> using namespace std; const int maxn=16; const int INF=100000000; int T,n,ans; int A[maxn][maxn],B[maxn][maxn]; int min(int a,int b){ return a<b?a:b;} int fun(string s) { int cnt=0,i,j,sum; for(i=0;i<n;i++) B[0][i]=s[i]-'0'; for(i=1;i<n;i++) for(j=0;j<n;j++) B[i][j]=A[i][j]; for(i=0;i<n;i++) { for(j=0;j<n;j++) { sum=0; if(i>0) sum+=B[i-1][j]; if(j>0) sum+=B[i][j-1]; if(j<n-1) sum+=B[i][j+1]; if(i<n-1 && sum%2==0 && B[i+1][j]==1) return INF; if(i<n-1 && sum%2==1 && B[i+1][j]==0) B[i+1][j]='1'; if(i==n-1 && sum%2==1) return INF; } } for(i=0;i<n;i++) { for(j=0;j<n;j++) if(B[i][j]!=A[i][j]) cnt++; } return cnt; } void dfs(string s,int i) { if(i==n) { ans=min(ans,fun(s)); return ; } if(s[i]=='0') { dfs(s,i+1); s[i]='1'; dfs(s,i+1); s[i]='0'; } else dfs(s,i+1); } void solve() { ans=INF; string s=""; for(int i=0;i<n;i++) s+=A[0][i]+'0'; dfs(s,0); if(ans==INF) ans=-1; printf("%d\n",ans); } int main() { int icase=0; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) scanf("%d",&A[i][j]); } printf("Case %d: ",++icase); solve(); } return 0; }
相关文章推荐
- UVA - 11464 Even Parity
- UVA 11464 Even Parity(递归枚举)
- UVA - 11464 Even Parity 01矩阵——枚举
- uva 11464 - Even Parity(暴力枚举)
- UVa_11464_Even Parity_机智地搜索
- UVA 11464 Even Parity
- 【UVA】11464-Even Parity(二进制枚举子集)
- uva 11464 Even Parity 模拟 二进制枚举
- UVA 11464 - Even Parity(枚举方法)
- UVA 11464 Even Parity (独特思路)
- UVa 11464 - Even Parity
- UVA 11464-Even Parity(偶数矩阵-开关问题)
- UVa 11464 - Even Parity(枚举)
- UVA 11464 Even Parity
- UVA11464 Even Parity
- UVA 11464 Even Parity
- UVa11464 Even Parity
- UVA11464 Even Parity (局部枚举定全局枚举)
- UVa-11464 Even Parity
- UVA 11464 - Even Parity(枚举方法)