uva 11464 Even Parity(逐层递推)
2015-01-26 13:33
113 查看
uva 11464 Even Parity
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:
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
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
Output for Sample Input
Case 1: 0
Case 2: 3
Case 3: -1
题目大意:给你一个 n * n 的 01 矩阵,现在你的任务是将这个矩阵中尽量少的 0 转化为 1 ,使得每个数的上下左右四个相邻的数加起来是偶数。求最少的转化个数。
解题思路(转):
首先,n 的规模并不大,最大只有15。但是完全枚举整个矩阵显然是不可能的(2225 ≈ 5 * 1067)。但是我们可以枚举第一行,然后用第一行来算出后面的所有行。
但是,怎么算呢?
先来说下算法。对于每一行,我们通过他上面的两行来决定他的值。如果上面两行得到值为奇数,那么这一行就赋值为 1 ,否则赋值为 0 。 然后与原始矩阵比较,如果是由 1 变 0 那么这种情况是不允许的,于是继续枚举第一行,重新计算后面的。如果是由 0 变 1(或不变) 那么保存下来。最后在计算所有的格子之后,遍历一下就能统计转化数。然后取最小值就可以了。
下面用题目中给的第二组数据来演示一下如何通过上一行来计算下一行。
问题 正解
0 0 0 ==> 0 1 0
1 0 0 ==> 1 0 1
0 0 0 ==> 0 1 0
来举例说明。首先我们通过枚举第一行可以达到以下情况
0 | 1 | 0 |
0 | 1 | 0 |
x | ||
0 | 1 | 0 |
1 | x | |
0 | 1 | 0 |
1 | 0 | x |
0 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 0 |
#include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> #define INF 1 << 30 using namespace std; int A[20][20], B[20][20]; int check(int n, int s) { memset(B, 0, sizeof(B)); for (int i = 0; i < n; i++) { if (s & (1 << i)) { B[0][i] = 1; } else if (A[0][i] == 1) return INF; } for (int i = 1; i < n; i++) { for (int j = 0; j < n; j++) { int sum = 0; if (i > 1) sum += B[i - 2][j]; if (j > 0) sum += B[i - 1][j - 1]; if (j < n - 1) sum += B[i - 1][j + 1]; if (sum % 2) B[i][j] = 1; if (B[i][j] == 0 && A[i][j] == 1) return INF; } } int cnt = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (A[i][j] != B[i][j]) cnt++; } } return cnt; } int main() { int T, cnt = 1; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &A[i][j]); } } int ans = INF; for (int i = 0; i < (1 << n); i++) { ans = min(ans, check(n, i)); } if (ans == INF) ans = -1; printf("Case %d: %d\n", cnt++, ans); } return 0; }
相关文章推荐
- UVa11464 - Even Parity
- UVA11464 Even Parity
- UVa 11464 - Even Parity
- 暴力枚举- uva11464 - Even Parity
- UVa 11464 Even Parity
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
- Uva 11464 - Even Parity
- 【思维】uva11464 Even Parity 偶数矩阵
- uva 11464 - Even Parity(3级)
- uva 11464 - Even Parity
- Uva11464 - 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(p15)----Even Parity
- uva 11464 - Even Parity(3级)
- uva11464 - Even Parity