状态压缩+枚举 UVA 11464 Even Parity
/*
题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数
状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么
最后检查不同的数量,取最小值
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
int a[MAXN][MAXN], b[MAXN][MAXN];
int n; int check(int s) {
memset (b, , sizeof (b));
for (int i=; i<n; ++i) {
if (s & ( << i)) b[][i] = ;
else if (a[][i] == ) return INF;
} for (int i=; i<n; ++i) {
for (int j=; j<n; ++j) {
int sum = ;
if (i > ) sum += b[i-][j];
if (j > ) sum += b[i-][j-];
if (j < n-) sum += b[i-][j+];
b[i][j] = sum % ;
if (a[i][j] == && b[i][j] == ) return INF;
}
} int ret = ;
for (int i=; i<n; ++i) {
for (int j=; j<n; ++j) {
if (a[i][j] != b[i][j]) ret++;
}
} return ret;
} int main(void) { //UVA 11464 Even Parity
//freopen ("G.in", "r", stdin); int t, cas = ; scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
for (int i=; i<n; ++i) {
for (int j=; j<n; ++j) {
scanf ("%d", &a[i][j]);
}
} int ans = INF;
for (int i=; i<(<<n); ++i) {
ans = min (ans, check (i));
} if (ans == INF) ans = -;
printf ("Case %d: %d\n", ++cas, ans);
} return ;
}
状态压缩+枚举 UVA 11464 Even Parity的更多相关文章
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- POJ 1873 UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)
题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a ...
- UVA.11464 Even Parity (思维题 开关问题)
UVA.11464 Even Parity (思维题 开关问题) 题目大意 给出一个n*n的01方格,现在要求将其中的一些0转换为1,使得每个方格的上下左右格子的数字和为偶数(如果存在的话),求使得最 ...
- codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...
- hdu 4033 状态压缩枚举
/* 看别人的的思路 搜索搜不出来我太挫了 状态压缩枚举+好的位置 */ #include<stdio.h> #include<string.h> #define N 20 i ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- 洛谷P1036 选数 题解 简单搜索/简单状态压缩枚举
题目链接:https://www.luogu.com.cn/problem/P1036 题目描述 已知 \(n\) 个整数 \(x_1,x_2,-,x_n\) ,以及 \(1\) 个整数 \(k(k& ...
- 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 in ...
- UVALive 3953 Strange Billboard (状态压缩+枚举)
Strange Billboard 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/A Description The marke ...
随机推荐
- Mycat环境搭建教程收集(待实践)
先收集,后续再实践. http://blog.csdn.net/dreamcode/article/details/44307377 http://blog.csdn.net/lanonola/art ...
- mybatis resultmap标签type属性什么意思
mybatis resultmap标签type属性什么意思? :就表示被转换的对象啊,被转换成object的类型啊 <resultMap id="BaseResultMap" ...
- A* Pathfinding Project (Unity A*寻路插件) 使用教程
Unity4.6 兴许版本号都已经内置了寻路AI了.之前的文章有介绍 Unity3d 寻路功能 介绍及项目演示 然而两年来项目中一直使用的是 A* Pathfinding 这个插件的.所以抽时间来写下 ...
- VC++ 提示无法打开包括文件“iostream.h”怎么办
把 //#include "iostream.h" 改成 #include<iostream> using namespace std; ...
- yarn之安装依赖包
安装依赖关系 yarn install用于安装项目的所有依赖项.依赖关系从您的项目package.json文件中检索,并存储在yarn.lock文件中. 开发包时,安装依赖关系最常见的是在 您刚刚检出 ...
- jqury-validate表单验证
首先需要引入插件:jquery.validate.js这个插件. 然后对需要验证的表单实现js: $("#add-firewalls-form").validate({ submi ...
- ASP.NET MVC不可或缺的部分——DI(IOC)容器及控制器重构的剖析
ASP.NET MVC不可或缺的部分——DI(IOC)容器及控制器重构的剖析 IoC框架最本质的东西:反射或者EMIT来实例化对象.然后我们可以加上缓存,或者一些策略来控制对象的生命周期,比如是否 ...
- opencv对图像进行边缘及角点检測
opencv对图像进行边缘及角点检測 先看结果: 代码: // ConsoleApplication1_812.cpp : Defines the entry point for the consol ...
- Canny算法源码,欢迎交流
http://blog.csdn.net/jianxiong8814/article/details/1563109 http://blog.csdn.net/assuper/article/deta ...
- OpenCV 2.4.8 or OpenCV 2.4.9组件结构全解
近期发现了一个好的opencv博客,准备依照他的顺序系统学习opencv,而且会一一转载过来 之前啃了不少OpenCV的官方文档,发现假设了解了一些OpenCV总体的模块架构后.再重点学习自己感兴趣的 ...