状态压缩+枚举 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 ...
随机推荐
- 家的范围 Home on the Range(洛谷 2733)
题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因为一些原因,他的奶牛只在正方形的牧场上吃草.)遗憾的是,他的奶牛已经毁坏一些土地.( 一 ...
- JVM 总结
面试 java 虚拟机 jvm 基础 jvm Write Once Run EveryWhere >jar 包可以在任何兼容jvm上运行 >jvm 适配器 屏蔽掉底层差异 >内存管理 ...
- HDU——1498 50 years, 50 colors
50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- java读取大文本文件
原文:http://blog.csdn.net/k21325/article/details/53886160 小文件当然可以直接读取所有,然后放到内存中,但是当文件很大的时候,这个方法就行不通了,内 ...
- 在虚拟机搭建JStrom
原文:http://blog.csdn.net/u014134180/article/details/51810311 一 安装步骤 二 搭建Zookeeper集群 1 ZooKeeper 单机安装与 ...
- 用pc构建DIY计算集群
-----------------------------------------------------------------用pc构建DIY计算集群目录/构建计算集群|-- /0前言|-- /1 ...
- Lua:ipairs和pairs的区别
ipairs 和pairs在lua中都是遍历tbale的函数但是两者有区别 1.pairs遍历table中的所有的key-vale 而ipairs会根据key的数值从1开始加1递增遍历对应的table ...
- matlab Newton method
% Matlab script to illustrate Newton's method % to solve a nonlinear equation % this particular scri ...
- android几个高速打包命令
在MTK android4.4 上,有几条命令能够高速打包system uaserdata bootimage,可提高效率. snod:这个命令是能够高速打包system.是不会运行android各 ...
- 8.跟我学solr---UpdateRequestProcessor具体解释
简单介绍 java web开发的同学应该非常熟悉,在开发中常常会使用filter来处理请求中的一些切面需求. solr也提供类似的一种链式结构的handler来满足在加入数据索引请求的时候.通过切片的 ...