题意:给一个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. java outterLoop跳出多重循环用法以及详解

    List<CommResultMsg> listresult=new ArrayList<CommResultMsg>(); outterLoop :for (int i = ...

  2. 面向对象的多态性(C++)

    以C++为例三大特效:封装.继承.多态,面向对象的编程语言都具有这些特性. 那么本节来谈谈多态性,尽量说的简单些容易理解! 多态什么意思?即运行时多态,以相同的方式处理不同类型的对象,产生不同的结果! ...

  3. <5>Lua多返回值和require模块

    1.多返回值 --1: 一个lua函数可以返回多个返回值: --2: 定义多个变量来接受多返回值 --3: lua的unpack函数,解开表里的单个的值; 结果 2.require模块 --1: 第一 ...

  4. DataSet 读取xml 报错有非法字符

    private void Bind() { string strLogPath = ConfigurationSettings.AppSettings["LOG_PATH"].To ...

  5. Windows10上安装Keras 和 TensorFlow-GPU

    安装环境: Windows 10 64bit GPU: GeForce gt 720 Python: 3.5.3 CUDA: 8 首先下载Anaconda3的Win10 64bit版,安装Python ...

  6. C# SQLite 数据库操作

    C# SQLite 数据库操作学习 运行环境:Window7 64bit,.NetFramework4.61,C# 7.0 参考: SQLite 官网 SQL As Understood By SQL ...

  7. Robot Framework 教程 (3) - Resource及关键字 的使用

    From:http://www.cnblogs.com/buaawp/p/4754399.html Robot Framework 教程 (3) - Resource及关键字 的使用 在进行软件自动化 ...

  8. codeforces 979B Treasure Hunt

    题意: 给出三个字符串,每个字符串长度相同,给出n,要求在n轮内,每一个字符串必须改变一个字符. 问最后哪个字符串中拥有最多相同的字符,即美丽度最大. 思路: 首先,很不容易想到的一点是从a变到a,有 ...

  9. uvalive 5731 Qin Shi Huang’s National Road System

    题意: 秦始皇要修路使得所有的城市连起来,并且花费最少:有一个人,叫徐福,他可以修一条魔法路,不花费任何的钱与劳动力. 秦始皇想让修路的费用最少,但是徐福想要受益的人最多,所以他们经过协商,决定让 A ...

  10. 20155228 实验一《Java开发环境的熟悉》实验报告

    20155228 实验一<Java开发环境的熟悉>实验报告 实验内容 使用JDK编译.运行简单的Java程序: 使用IDEA 编辑.编译.运行.调试Java程序. 实验要求 没有Linux ...