状态压缩+枚举 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 ...
随机推荐
- node.js里的buffer常见操作,copy,concat等实例讲解
//通过长度构建的buffer内容是随机的 var buffer=new Buffer(100); console.log(buffer); //手动清空buffer,一般创建buffer不会清空 b ...
- 51nod 1010 只包含因子2 3 5的数 && poj - 1338 Ugly Numbers(打表)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 http://poj.org/problem?id=1338 首先 ...
- codevs——1019 集合论与图论
1019 集合论与图论 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 集合论与图论对于小松来说 ...
- RabbitMQ集群环境搭建教程收集(待实践)
先收集,后续再实践. http://www.linuxidc.com/Linux/2016-10/136492.htm http://www.cnblogs.com/lion.net/p/572547 ...
- Servlet表单数据处理
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/form-data.html: 当需要从浏览器到Web服务器传递一些信息并最终传回到后台程序时,一 ...
- php-fpm配置项
php版本:php-7.3 全局配置 ;;;;;;;;;;;;;;;;;;;;; ; FPM Configuration ; ;;;;;;;;;;;;;;;;;;;;; ; All relative ...
- IOS程序崩溃报告管理解决方案(Crashlytics 在2014-09-24)
预研Crashlytics 在2014-09-241:实现原理在原理上,Crashlytics通过以下2步完成崩溃日志的上传和分析:(1)提供应用SDK,你需要在应用启动时调用其SDK来设置你的应用 ...
- CI知识:GitLab
Gitlab简介 GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 可通过Web界面进行访问公开的或者私人项目.它拥有与Github类似的 ...
- Hdu2111
<span style="color:#6600cc;">/* J - Saving HDU Time Limit:1000MS Memory Limit:32768K ...
- SSL和SSH的差别
有人说,SSH通常是用来提供安全的登录用的.SSL仅仅是一个在协议层中增加的一层用来提供安全. SSH工作在TCP之上,能够在启动一个SSH应用后.在其通道里执行其他协议的应用.如邮件. ...