UVA 10318 Security Panel(DFS剪枝 + 状压 + 思维)题解
题意:给一个r*c的矩阵开关(初始全打开的),每次按下一个开关都会改变3*3范围内的有*的地方的状态,问你最少几步能让开关全闭上,按升序输出按哪些按钮
思路:每个按钮至多按一下,按按钮的顺序和结果无关。我们把当前矩阵的开关状态状压到 long long,并且预处理按下每个按钮的变化,这样可以直接异或得到新的状态。这里还需要剪枝,因为顺序无关,我们直接从1按到r*c,那么如果我们按到第k行,现在就已经影响不到k-2行了,所以k-2行如果有开关没闭上就return。
代码:
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 1e5 + ;
const int MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
int r, c;
char s[][];
ll change[], ansStep[], step[], vis[][], lit, ansCnt, cnt, OK;
void init(int x, int y){
int pos = (x - ) * c + y;
change[pos] = ;
for(int i = ; i <= ; i++){
for(int j = ; j <= ; j++){
if(s[i][j] == '.') continue;
int xx = x + i - , yy = y + j - ;
if(xx < || xx > r || yy < || yy > c) continue;
ll p = (xx - ) * c + yy;
change[pos] += (1LL << p);
}
}
}
void dfs(int pos){
if(pos > r * c) return;
int x = pos / c, y = pos % c;
if(x >= ){
for(int i = ; i <= c; i++){
int pos2 = (x - ) * c + i;
if(!(lit & (1LL << pos2))) return;
}
}
lit ^= change[pos];
vis[x][y] = ;
step[cnt++] = pos;
if(lit == OK){
if(cnt < ansCnt){
for(int i = ; i < cnt; i++)
ansStep[i] = step[i];
ansCnt = cnt;
lit ^= change[pos];
vis[x][y] = ;
cnt--;
return;
}
}
dfs(pos + );
lit ^= change[pos];
vis[x][y] = ;
cnt--;
dfs(pos + );
}
int main(){
int ca = ;
while(~scanf("%d%d", &r, &c) && r + c){
OK = ;
for(int i = ; i <= ; i++) scanf("%s", s[i] + );
for(int i = ; i <= r; i++){
for(int j = ; j <= c; j++){
init(i, j);
OK += (1LL << ((i - ) * c + j));
}
}
ansCnt = ;
memset(vis, , sizeof(vis));
dfs();
printf("Case #%d\n", ca++);
if(ansCnt == ) printf("Impossible.\n");
else{
for(int i = ; i < ansCnt; i++){
if(i != ) printf(" ");
printf("%d", ansStep[i]);
}
printf("\n");
}
}
return ;
}
UVA 10318 Security Panel(DFS剪枝 + 状压 + 思维)题解的更多相关文章
- Codeforces 293B Distinct Paths DFS+剪枝+状压
目录 题面 题目链接 题意翻译 输入输出样例 输入样例#1 输出样例#1 输入样例#2 输出样例#2 输入样例#3 输出样例#3 输入样例#4 输出样例#4 说明 思路 AC代码 总结 题面 题目链接 ...
- UVa 10318 Security Panel
题意:给你一个3*3的翻转模版,深色部分表示翻转,浅色部分不变.然后你可以在r*c的矩形里依照模版进行翻转,要求所有点亮所有块.输出最小的步骤. 思路:有一点比较好想.每个块至多被翻转一次,翻两次的效 ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
- 【UVa】Headmaster's Headache(状压dp)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Coco dfs 或者 状压dp。...
C -- Coco Time Limit:1s Memory Limit:64MByte Submissions:148Solved:85 DESCRIPTION Coco just learned ...
- 【UVA】11825 Hackers' Crackdown(状压dp)
题目 传送门:QWQ 分析 $ n<= 16 $ 显然是状压 然后搞一搞(靠着蓝书yy一下) 代码 #include <bits/stdc++.h> using namespace ...
- 2018.8.1 状压 CF482C 题解
noip2016考了一道状压dp,一道期望dp 然而这题是状压期望dp... 所以难度是什么,省选noi吗... 怎么办... 题目大意: 给定n个字符串,甲从中任选出一个串(即选出每个串的概率相同为 ...
- Allowed Letters CodeForces - 1009G(状压思维)
题意: 给出一个字符串 给出几个定点必须是哪个字母(或者是几个字母中的一个) 然后求在满足所有定点后的最小字符串 解析: 没错 这题是暴力 用状压暴力 “a - f” 用”0 - 5“ 这几个数字代 ...
- UVa 208 消防车(dfs+剪枝)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- opencv的安装及填坑
opencv的配置方式: https://blog.csdn.net/cocoaqin/article/details/78163171 输入Python时候报错: ERROR: ld.so: obj ...
- notepad去掉空行
选择替换,把查找模式设置为正则表达式,在查找框中自己输入 ^\s+ ,替换框留空,点“全部替换”,即可(先全选).注意:不要复制我的,自己输入,且用英文格式输入.
- ReactiveCocoa(III)
flatMap(FlattenStrategy.latest) observe(on: UIScheduler()).startWithResult 切换线程: observeOn(UISchedul ...
- Spring源码阅读(六)
这一讲分析spring bean属性注入代码populateBean,源码分析如下 /** * Populate the bean instance in the given BeanWrapper ...
- over(partition by)开窗函数的使用
开窗函数是分析函数中的一种,开窗函数与聚合函数的区别是:开窗函数是用于计算基于组的某种聚合值且每个的组的聚合计算结果可以有多行,而聚合函数每个组的聚合计算结果只有一个.使用开窗函数可以在没有group ...
- 20165215 2017-2018-2 《Java程序设计》第7周学习总结
20165215 2017-2018-2 <Java程序设计>第七周学习总结 教材学习内容总结 chapter11 下载安装MySQL服务器 启动MySQL数据库服务器 在bin子目录中, ...
- Linux基础命令---显示登录用户w
w 显示哪些用户登录,并且显示用户在干什么.报头按此顺序显示当前时间.系统运行时间.当前登录用户数以及过去1.5和15分钟的系统平均负载.接着为每个用户显示以下条目:登录名.TTY名称.远程主机.登录 ...
- 转:[你必须知道的异步编程]C# 5.0 新特性——Async和Await使异步编程更简单
本专题概要: 引言 同步代码存在的问题 传统的异步编程改善程序的响应 C# 5.0 提供的async和await使异步编程更简单 async和await关键字剖析 小结 一.引言 在之前的C#基础知 ...
- JS实现document.ready
通常我们想要在页面内容加载完成后运行 JS 时,都会使用 window.onload 来处理,比如: window.onload = function(){ alert('Hello World!') ...
- 计蒜客---N的-2进制表示
对于十进制整数N,试求其-2进制表示. 例如,因为 1*1 + 1*-2 + 1*4 + 0*-8 +1*16 + 1*-32 = -13 ,所以(-13)_10 = ( ...