题目

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 <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <tuple>
#include <cassert> using namespace std;
#define LOCAL_DEBUG
#define NOW_QUEUE (ques[queId])
#define LAST_QUEUE (ques[1 - queId])
const int MAXN = ;
struct Node{
int lineSta;
int lineSum;
int changed;
Node(int _lineSta, int _lineSum, int _changed) {
lineSta = _lineSta;
lineSum = _lineSum;
changed = _changed;
}
};
queue<Node> ques[];
int queId;
int orgStatus[MAXN][MAXN];
int stackedStatus[MAXN];
int statusLimit; int getLineSum(int formerlineSta, int lineSta) {
return (formerlineSta ^ (lineSta >> ) ^ (lineSta << )) & statusLimit;
} int getChanged(int lineno, int lineSta) {
int changedSta = stackedStatus[lineno] ^ lineSta;
int changed = ;
while (changedSta > ) {
changed++;
changedSta -= (changedSta & (-changedSta));
}
return changed;
} int main() {
#ifdef LOCAL_DEBUG
freopen("input.txt", "r", stdin);
//freopen("output2.txt", "w", stdout);
#endif // LOCAL_DEBUG
int T;
scanf("%d", &T);
for (int ti = ; ti <= T; ti++) {
queId = ; int n;
scanf("%d", &n);
for (int i = ; i < n; i++) {
stackedStatus[i] = ;
for (int j = ; j < n; j++) {
scanf("%d", orgStatus[i] + j);
stackedStatus[i] = stackedStatus[i] * + orgStatus[i][j];
}
} statusLimit = ( << n) - ;
for (int sta = ; sta <= statusLimit; sta++) {
if((sta & stackedStatus[]) == stackedStatus[])NOW_QUEUE.push(Node(sta, getLineSum(, sta), getChanged(, sta)));
}
for (int i = ; i < n; i++) {
queId = - queId;
while (!LAST_QUEUE.empty()) {
Node node = LAST_QUEUE.front(); LAST_QUEUE.pop();
Node newNode(node);
newNode.lineSta = stackedStatus[i];
int lastLineSum = node.lineSum ^ stackedStatus[i];
bool fl = true;
for (int j = ; j < n && fl; j++) {
if (lastLineSum & ( << j)) {
if (stackedStatus[i] & ( << j)) { fl = false; break; }
else {
newNode.lineSta |= << j;
}
}
}
if (fl) {
newNode.lineSum = getLineSum(node.lineSta, newNode.lineSta);
newNode.changed = node.changed + getChanged(i, newNode.lineSta);
NOW_QUEUE.push(newNode);
}
}
}
int ans = n * n + ;
while (!NOW_QUEUE.empty()) {
Node node = NOW_QUEUE.front(); NOW_QUEUE.pop();
ans = min(ans, node.changed);
}
if (ans > n * n)printf("Case %d: -1\n", ti);
else printf("Case %d: %d\n", ti, ans);
} return ;
}

UVA 11464 - Even Parity 状态压缩,分析 难度: 2的更多相关文章

  1. 状态压缩+枚举 UVA 11464 Even Parity

    题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...

  2. UVA.11464 Even Parity (思维题 开关问题)

    UVA.11464 Even Parity (思维题 开关问题) 题目大意 给出一个n*n的01方格,现在要求将其中的一些0转换为1,使得每个方格的上下左右格子的数字和为偶数(如果存在的话),求使得最 ...

  3. 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 ...

  4. UVA 1508 - Equipment dp状态压缩

    题意:  已知n个5元组,从中选出k组,使得这些组中5个位置,每个位置上最大数之和最大. 分析:当k>5时,就是n个5元组最大的数之和,当k<5时,就当做5元组,状态压缩,用00000表示 ...

  5. UVA 11464 Even Parity(部分枚举 递推)

    Even Parity We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a on ...

  6. UVA 11464 - Even Parity(枚举方法)

    D Even Parity Input: Standard Input Output: Standard Output We have a grid of size N x N. Each cell ...

  7. POJ 1753 Flip Game 状态压缩,暴力 难度:1

    Flip Game Time Limit: 1000MS  Memory Limit: 65536K  Total Submissions: 4863  Accepted: 1983 Descript ...

  8. UVA 10651 Pebble Solitaire 状态压缩dp

    一开始还在纠结怎么表示一个状态,毕竟是一个串.后来搜了一下题解发现了这里用一个整数的前12位表示转态就好了 ,1~o,0~'-',每个状态用一个数来表示,然后dp写起来就比较方便了. 代码: #inc ...

  9. UVA 11464 Even Parity (独特思路)

    题意:有一个n*n的01矩阵,任务是把尽可能少的0变成1,使得每个元素的上.下.左.右元素之和为偶数. 思路:很容易想到的思路是枚举每个点是0还是1,因为n<=15,复杂度就是2^225显然TL ...

随机推荐

  1. Centos 7 Docker安装配置

    版本介绍 Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE.社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比如经过官方测试认证过的基础设 ...

  2. Cocos Creator 脚本模板

    1.由于新建Cocos Creator脚本带有很多注释,并且有时候需要增加定制的默认注释,所以需要修改脚本生成模板. 2.在CocosCreator\resources\static\template ...

  3. Java创建多线程和线程安全集合Vector

    public class Test { public static Vector<String> data = new Vector<String>(); public sta ...

  4. python中的进程和线程

    什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本 ...

  5. (转)C# Assembly.Load 使用

    在C#中,我们要使用反射,首先要搞清楚以下命名空间中几个类的关系: System.Reflection命名空间(1)  AppDomain:应用程序域,可以将其理解为一组程序集的逻辑容器(2)  As ...

  6. Kaggle泰坦尼克数据科学解决方案

    原文地址如下: https://www.kaggle.com/startupsci/titanic-data-science-solutions --------------------------- ...

  7. English trip V1 - 11.What's That? 那是什么?Teacher:Patrick Key:There's/There are

    In this lesson you will learn to describe the position of an object. 在本课中,您将学习如何描述对象的位置. 课上内容(Lesson ...

  8. MySql常用函数 --MySql

    1.目标 MySQL数据库中提供了很丰富的函数.MySQL函数包括数学函数.字符串函数.日期和时间函数.条件判断函数.系统信息函数.加密函数.格式化函数等.通过这些函数,可以简化用户的操作.例如,字符 ...

  9. 遍历input文本框

    最近写的一个项目中,页面中有很多的“text文本框”和“select下拉框” 校验input框和select框是否非空,如果为空给出提示.反之,隐藏提示内容. html  页面中的input类型有ty ...

  10. 【JS】【3】标签显示几秒后自动隐藏

    $("#XXX").show().delay(2000).hide(0); 2000,0:可选,速度,(毫秒:"slow":"fast") ...