状态压缩+枚举 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 ...
随机推荐
- 郁闷的出纳员(bzoj 1503)
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...
- 2018/3/3 解析ThreadLocal源码
今天听到一个老哥说道ThreadLocal在源码设计上面的一些好处,于是决定把ThreadLocal源码彻底分析一下. 首先,我们来看下set方法 可以看到,这个方法里,先获得了当前线程,之后将当前线 ...
- JFileChooser 中文API
javax.swing类 JFileChooser java.lang.Object java.awt.Component java.awt.Container javax.swing.JCompon ...
- Why we have tuple and list in python
The most notable difference between tuple and list is that tuple is immutable and list is mutable. B ...
- 智能眼镜技术科普:VR、AR、MR的区别
前段时间, 获得谷歌5亿美元融资的技术公司Magic Leap在WSJD展会中放出了一段实录视频,引起不小骚动.如今,也有媒体称他们为MR公司,那么VR.AR.MR之间到底有什么区别呢. VR.AR. ...
- CSS3的animation功能
旋转动画 <img src="https://facebook.github.io/react/img/logo.svg" class="App-logo" ...
- linux网络编程中的shutdown()与close()函数
1.close()函数 int close(int sockfd); //返回成功为0,出错为-1 close 一个套接字的默认行为是把套接字标记为已关闭,然后立即返回到调用进程,该套接字不能再由cl ...
- HDOJ 4259 Double Dealing
找每一位的循环节.求lcm Double Dealing Time Limit: 50000/20000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 李洪强iOS开发之动态获取UILabel的bounds
李洪强iOS开发之动态获取UILabel的bounds 在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需 ...
- VELT-0.1.6开发:载入根文件系统
快乐虾 http://blog.csdn.net/lights_joy/(QQ群:Visual EmbedLinux Tools 375515651) 欢迎转载,但请保留作者信息 VELT的全称是Vi ...