UVA11464 Even Parity (局部枚举定全局枚举)
来源:互联网 发布:香港域名注册商 编辑:程序博客网 时间:2024/04/30 02:22
We have a grid of size
N
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
4:
1
0
1
0
The parity of each cell would be
1
3
1
2
1
1
1
1
2
3
2
1
0
1
0
0
2
1
2
1
0
0
0
0
0
1
0
0
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 rst 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.
SampleInput
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
SampleOutput
Case 1: 0
Case 2: 3
Case 3: -1
#include<cstdio>#include<cstring>const int maxn=15;const int inf=0x3f3f3f3f;int a[maxn][maxn];int now[maxn][maxn];int n;inline int get(int xi,int yi) { int t=0; if(xi>=1)t+=a[xi-1][yi]; if(xi+1<n)t+=a[xi+1][yi]; if(yi>=1)t+=a[xi][yi-1]; if(yi+1<n)t+=a[xi][yi+1]; return t;}int main() { int T; #ifdef tangge freopen("11464.txt","r",stdin); #endif // tangge scanf("%d",&T); for(int cas=1; cas<=T; ++cas) { scanf("%d",&n); for(int i=0; i<n; ++i) { for(int j=0; j<n; ++j) { scanf("%d",&a[i][j]); } } int sum=1<<n; memcpy(now,a,sizeof(a)); int best=inf,t=0; for(int i=0; i<sum; ++i) { t=0; memcpy(a,now,sizeof(a)); for(int j=0; j<n; ++j) { if((i>>j)&1) { if(!a[0][j])a[0][j]=1,++t; } } if(t>=best)continue; bool J=true; for(int j=1; j<n&&J; ++j) { for(int k=0; k<n&&J; ++k) { if(get(j-1,k)&1){ if(!a[j][k])a[j][k]=1,++t; else J=false; } } } for(int j=0;j<n&&J;++j){ if(get(n-1,j)&1)J=false; } if(J&&(best>t)){ best=t; } } printf("Case %d: %d\n",cas,best==inf?-1:best); } return 0;}
- UVA11464 Even Parity (局部枚举定全局枚举)
- uva11464 - EVen Parity (枚举)
- 暴力枚举- uva11464 - Even Parity
- poj3279(局部枚举定全局)
- Uva11464 - Even Parity
- uva11464 - Even Parity
- UVA11464 Even Parity
- UVA11464 Even Parity
- Uva11464 - Even Parity
- uva11464 Even Parity
- UVa11464-Even Parity
- UVA11464 BNU19882 Even Parity
- uva11464 Even Parity
- UVA - 11464 Even Parity 枚举
- UVA 11464 Even Parity(枚举)
- uva11464 - Even Parity(偶数矩阵)
- uva 11464 - Even Parity(暴力枚举)
- UVA 11464 - Even Parity(枚举方法)
- 华为上机题汇总(四)
- Swift单例的写法
- Android逆向分析之Cydia
- DirectShow之接口实战篇_多媒体播放器
- C标签和s标签的对比,特别是里面的if判断条件的写法
- UVA11464 Even Parity (局部枚举定全局枚举)
- linux shell脚本编程
- HTML5-手勢
- yun2win发布即时通讯云IM,做最安全的即时通讯云!
- 杭电 hdu1251 统计难题 字典树
- 关于共交车对黄色座位的思考
- JAVA字符串转日期或日期转字符串
- 这个夏天的我
- Neither BindingResult nor plain target object for bean name 'command' available as request attribute