题意:给一个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剪枝 + 状压 + 思维)题解的更多相关文章

  1. Codeforces 293B Distinct Paths DFS+剪枝+状压

    目录 题面 题目链接 题意翻译 输入输出样例 输入样例#1 输出样例#1 输入样例#2 输出样例#2 输入样例#3 输出样例#3 输入样例#4 输出样例#4 说明 思路 AC代码 总结 题面 题目链接 ...

  2. UVa 10318 Security Panel

    题意:给你一个3*3的翻转模版,深色部分表示翻转,浅色部分不变.然后你可以在r*c的矩形里依照模版进行翻转,要求所有点亮所有块.输出最小的步骤. 思路:有一点比较好想.每个块至多被翻转一次,翻两次的效 ...

  3. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

  4. 【UVa】Headmaster's Headache(状压dp)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. Coco dfs 或者 状压dp。...

    C -- Coco Time Limit:1s Memory Limit:64MByte Submissions:148Solved:85 DESCRIPTION Coco just learned ...

  6. 【UVA】11825 Hackers' Crackdown(状压dp)

    题目 传送门:QWQ 分析 $ n<= 16 $ 显然是状压 然后搞一搞(靠着蓝书yy一下) 代码 #include <bits/stdc++.h> using namespace ...

  7. 2018.8.1 状压 CF482C 题解

    noip2016考了一道状压dp,一道期望dp 然而这题是状压期望dp... 所以难度是什么,省选noi吗... 怎么办... 题目大意: 给定n个字符串,甲从中任选出一个串(即选出每个串的概率相同为 ...

  8. Allowed Letters CodeForces - 1009G(状压思维)

    题意: 给出一个字符串 给出几个定点必须是哪个字母(或者是几个字母中的一个)  然后求在满足所有定点后的最小字符串 解析: 没错 这题是暴力 用状压暴力 “a - f” 用”0 - 5“ 这几个数字代 ...

  9. UVa 208 消防车(dfs+剪枝)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. callback源码分析——callbacks

    uvm的callback必须是提供者有预见性的留一些方法在function前后,这样在使用的时候,进行遍历调度即可 设计者,需要从uvm_callback定义一个基类,只定义function原型,定义 ...

  2. [17]Windows的启动过程

    一.内核的引导 在intel x86系统上,windows操作系统获得控制首先从硬盘的主引导记录(MBR,Master Boot Record)开始,windows setup程序在安装windows ...

  3. C++中位运算

    简介 1 位逻辑运算符: & (位   “与”)  and ----------------- 2个都为1 才是1-----------0^0 = 0 , 0^1 = 0,  1^0 = 0  ...

  4. 强化学习---TRPO/DPPO/PPO/PPO2

    时间线: OpenAI 发表的 Trust Region Policy Optimization, Google DeepMind 看过 OpenAI 关于 TRPO后, 2017年7月7号,抢在 O ...

  5. 改装原生的dialog

    改装 dialog 定制 View rootView = LayoutInflater.from(mContext). inflate(R.layout.nfc_tag_name_dialog, nu ...

  6. Chess (SG + 状态压缩预处理)

    #include<bits/stdc++.h> #define bit(t) (1 << t) using namespace std; <<; ;//k是集合s的 ...

  7. 20155228 实验三 敏捷开发与XP实践

    20155228 实验三 敏捷开发与XP实践 实验内容 1. XP基础 2. XP核心实践 3. 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)>& ...

  8. Block 循环引用(中)

    不会造成循环引用的block 大部分GCD方法 dispatch_async(dispatch_get_main_queue(), ^{ [self doSomething]; }); 因为self并 ...

  9. Lua 服务器与客户端实例

    =============================================================== 服务器,main.lua ======================= ...

  10. 【Hbase学习之三】Hbase Java API

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-h ...