枚举 UVA 11464 Even Parity

题目传送门

 1 /*
 2     题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数
 3     枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么
 4             最后检查不同的数量,取最小值
 5 */
 6 #include <cstdio>
 7 #include <algorithm>
 8 #include <cstring>
 9 using namespace std;
10
11 const int MAXN = 20;
12 const int INF = 0x3f3f3f3f;
13 int a[MAXN][MAXN], b[MAXN][MAXN];
14 int n;
15
16 int check(int s) {
17     memset (b, 0, sizeof (b));
18     for (int i=0; i<n; ++i)    {
19         if (s & (1 << i))   b[0][i] = 1;
20         else if (a[0][i] == 1)  return INF;
21     }
22
23     for (int i=1; i<n; ++i)    {
24         for (int j=0; j<n; ++j)    {
25             int sum = 0;
26             if (i > 1)  sum += b[i-2][j];
27             if (j > 0)  sum += b[i-1][j-1];
28             if (j < n-1)  sum += b[i-1][j+1];
29             b[i][j] = sum % 2;
30             if (a[i][j] == 1 && b[i][j] == 0)   return INF;
31         }
32     }
33
34     int ret = 0;
35     for (int i=0; i<n; ++i)    {
36         for (int j=0; j<n; ++j)    {
37             if (a[i][j] != b[i][j]) ret++;
38         }
39     }
40
41     return ret;
42 }
43
44 int main(void)  {       //UVA 11464 Even Parity
45     //freopen ("G.in", "r", stdin);
46
47     int t, cas = 0;  scanf ("%d", &t);
48     while (t--) {
49         scanf ("%d", &n);
50         for (int i=0; i<n; ++i)    {
51             for (int j=0; j<n; ++j)    {
52                 scanf ("%d", &a[i][j]);
53             }
54         }
55
56         int ans = INF;
57         for (int i=0; i<(1<<n); ++i)    {
58             ans = min (ans, check (i));
59         }
60
61         if (ans == INF) ans = -1;
62         printf ("Case %d: %d\n", ++cas, ans);
63     }
64
65     return 0;
66 }
时间: 2024-08-21 20:08:19

枚举 UVA 11464 Even Parity的相关文章

uva 11464 Even Parity(逐层递推)

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). Su

UVA 11464 Even Parity(枚举递推)

11464 - Even Parity Time limit: 3.000 seconds 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,

UVa 11464 Even Parity

题意:给出n*n的01矩阵,将尽量少的0变成1,使得每个元素的上下左右的元素的和为偶数 看的白书的思路,二进制枚举第一行,再依次算出改变元素的个数, 自己写的时候发现这里不会写,“每个元素的上下左右的元素” 大概就是这个意思 真是太捉急了的说-----------5555 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<s

【转载】UVa 11464 Even Parity 偶数矩阵

题意:给你一个n*n的01矩阵,让你把这个矩阵中尽量少的0转换成1,使得矩阵每个位置的上下左右四个相邻的数加起来能被2整除,求最少的转换数 首先,n 的规模并不大,最大只有15.但是完全枚举整个矩阵显然是不可能的(2225 ≍ 5 * 1067).但是我们可以枚举第一行,然后用第一行来算出后面的所有行. 但是,怎么算呢? 先来说下算法.对于每一行,我们通过他上面的两行来决定他的值.如果上面两行得到值为奇数,那么这一行就赋值为 1 ,否则赋值为 0 . 然后与原始矩阵比较,如果是由 1 变 0 那

UVA 11464 - Even Parity 状态压缩,分析 难度: 2

题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2459 题意 N*N 的01方阵,可用操作为把任意0变为1,求操作的最小次数,使得任意位置的上下左右之和(不包含自身)为偶数 思路 如刘书,关键在于状态只有第一行的2^15个. 感想 1. 忘了memset 代码 #include <algorithm> #include

【UVA】11464-Even Parity(二进制枚举子集)

枚举第一行的所有可能情况,之后根据上面行计算下面行(判断是否冲突),获得最终结果. 14058243 11464 Even Parity Accepted C++ 0.275 2014-08-18 05:14:15 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<stack> #inc

uva 11464

/**  * @brief uva 11464  * @file 11464.cpp  * @author mianma  * @created 2014/12/16 17:53  * @edited  2014/12/16 17:53  * @type brute  * @note  */ #include <fstream> #include <iostream> #include <cstring> #include <cmath> using nam

Uva 11464 偶数矩阵

题目链接:https://uva.onlinejudge.org/external/114/11464.pdf 和开关问题类似,只不过现在是用的位运算操作更简单了,其中要注意的是,只能将0变成1. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define inf 0x3f3f3f3f 6 7 int a[20][20]; 8 int b[20][20]; 9 int n; 10 11 int dr[3] = {-1,0,0}

暴力枚举 UVA 10976 Fractions Again?!

题目传送门 1 /* 2 x>=y, 1/x <= 1/y, 因此1/k - 1/y <= 1/y, 即y <= 2*k 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <