HDU_4770 Lights Against Dudely 状压+剪枝
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770
Lights Against Dudely
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2021 Accepted Submission(s): 595
Hagrid: "Well there's your money, Harry! Gringotts, the wizard bank! Ain't no safer place. Not one. Except perhaps Hogwarts."
— Rubeus Hagrid to Harry Potter.
Gringotts Wizarding Bank is the only bank of the wizarding world, and is owned and operated by goblins. It was created by a goblin called Gringott. Its main offices are located in the North Side of Diagon Alley in London, England. In addition to storing money
and valuables for wizards and witches, one can go there to exchange Muggle money for wizarding money. The currency exchanged by Muggles is later returned to circulation in the Muggle world by goblins. According to Rubeus Hagrid, other than Hogwarts School
of Witchcraft and Wizardry, Gringotts is the safest place in the wizarding world.
The text above is quoted from Harry Potter Wiki. But now Gringotts Wizarding Bank is not safe anymore. The stupid Dudley, Harry Potter's cousin, just robbed the bank. Of course, uncle Vernon, the drill seller, is behind the curtain because he has the most
advanced drills in the world. Dudley drove an invisible and soundless drilling machine into the bank, and stole all Harry Potter's wizarding money and Muggle money. Dumbledore couldn't stand with it. He ordered to put some magic lights in the bank rooms to
detect Dudley's drilling machine. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1).....
A 3×4 bank grid is shown below:

Some rooms are indestructible and some rooms are vulnerable. Dudely's machine can only pass the vulnerable rooms. So lights must be put to light up all vulnerable rooms. There are at most fifteen vulnerable rooms in the bank. You can at most put one light
in one room. The light of the lights can penetrate the walls. If you put a light in room (x,y), it lights up three rooms: room (x,y), room (x-1,y) and room (x,y+1). Dumbledore has only one special light whose lighting direction can be turned by 0 degree,90
degrees, 180 degrees or 270 degrees. For example, if the special light is put in room (x,y) and its lighting direction is turned by 90 degrees, it will light up room (x,y), room (x,y+1 ) and room (x+1,y). Now please help Dumbledore to figure out at least how
many lights he has to use to light up all vulnerable rooms.
Please pay attention that you can't light up any indestructible rooms, because the goblins there hate light.
In each test case:
The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 200).
Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, and '.' means a vulnerable room.
The input ends with N = 0 and M = 0
If there are no vulnerable rooms, print 0.
If Dumbledore has no way to light up all vulnerable rooms, print -1.
2 2
##
##
2 3
#..
..#
3 3
###
#.#
###
0 0
0
2
-1
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cstdio>
#define MAX_N 205
#define MAX_M 205
#define MAX_L 20
using namespace std; char grid[MAX_N][MAX_M];
bool used[MAX_N][MAX_M];
int N,M;
int tot=0,x[MAX_L],y[MAX_L];
int dx[4]={1,1,-1,-1},dy[4]={1,-1,1,-1};
int ddx[4]={0,1,0,-1},ddy[4]={1,0,-1,0};
int can=0; struct node
{
int one,value;
}; node two[16][1<<15]; bool cmp(node a,node b)
{
return a.one<b.one;
} int main()
{
for(int i=0;i<16;i++)
{
for(int j=0;j<(1<<i);j++)
{
two[i][j].one=0;
for(int k=0;k<i;k++)
if((1<<k)&j)two[i][j].one++;
two[i][j].value=j;
}
sort(two[i],two[i]+(1<<i),cmp);
}
/*for(int i=0;i<5;i++,cout<<"------"<<endl)
for(int j=0;j<(1<<i);j++)
cout<<two[i][j].value<<" "<<two[i][j].one<<endl;*/
while(~scanf("%d%d",&N,&M))
{
int ans=100;
can=0;
if(N==0&&M==0)return 0;
memset(used,0,sizeof(used));
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
tot=0;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
{
char c;
scanf(" %c",&c);
grid[i][j]=c;
if(c=='.')
x[tot]=i,y[tot++]=j;
}
if(tot==0){printf("0\n");continue;}
bool fl=true;
for(int i=0;i<tot;i++)
{
int nearNum=0,daNum=0;
for(int j=0;j<4;j++)
{
int nnx=ddx[j]+x[i],nny=ddy[j]+y[i];
if(nnx>=0&&nnx<N&&nny>=0&&nny<M)
{
nearNum++;
daNum+=grid[nnx][nny]=='#';
}
}
//cout<<nearNum<<" "<<daNum<<endl;
if((nearNum==daNum)&&(nearNum>2)){printf("-1\n");fl=0;break;} int nx=x[i]+dx[2],ny=y[i]+dy[2];
if((nx>=0&&nx<N&&grid[nx][y[i]]=='#')||(ny>=0&&ny<M&&grid[x[i]][ny]=='#'))can&=~(1<<i);
else
can|=(1<<i);
} if(!fl)continue;
//cout<<can<<endl;
bool ffl=1;
for(int q=0;q<(1<<tot)&&ffl;q++)
for(int j=0;j<tot&&ffl;j++)
{
int i=two[tot][q].value;
int tmpCan=can|(1<<j);
if(!((1<<j)&i))continue;
if((tmpCan|i)>tmpCan)continue;
for(int k=0;k<4&&ffl;k++)
{
memset(used,0,sizeof(used));
int tmp=0,tmpAns=0;
bool flag=true;
for(int t=0;t<tot&&ffl;t++)
{
if(!((1<<t)&i))continue;
int nx=x[t]+(t==j?dx[k]:dx[2]),ny=y[t]+(t==j?dy[k]:dy[2]);
if(nx>=0&&nx<N&&grid[nx][y[t]]=='#'){flag=false;break;}
if(ny>=0&&ny<M&&grid[x[t]][ny]=='#'){flag=false;break;}
if(nx>=0&&nx<N){tmp+=!used[nx][y[t]];used[nx][y[t]]=1;}
if(ny>=0&&ny<M){tmp+=!used[x[t]][ny];used[x[t]][ny]=1;}
tmp+=!used[x[t]][y[t]];used[x[t]][y[t]]=1;
}
if(tmp==tot&&flag){ans=two[tot][q].one;ffl=0;break;}
}
}
//cout<<ans<<endl;
if(ans==100)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}
HDU_4770 Lights Against Dudely 状压+剪枝的更多相关文章
- 状压dfs小记
一点前(tu)言(cao) 真的考起dfs来可谓是什么都能往dfs上套 状压不止能dp,还能与dfs结合成为搜索好(duliu)题 剪枝卡常司空见惯(打开题解一看并不是纯dfs,emmmm) 开始正文 ...
- HDU 4770 Lights Against Dudely(暴力+状压)
思路: 这个题完全就是暴力的,就是代码长了一点. 用到了状压,因为之前不知道状压是个东西,大佬们天天说,可是我又没学过,所以对状压有一点阴影,不过这题中的状压还是蛮简单的. 枚举所有情况,取开灯数最少 ...
- UVA 10318 Security Panel(DFS剪枝 + 状压 + 思维)题解
题意:给一个r*c的矩阵开关(初始全打开的),每次按下一个开关都会改变3*3范围内的有*的地方的状态,问你最少几步能让开关全闭上,按升序输出按哪些按钮 思路:每个按钮至多按一下,按按钮的顺序和结果无关 ...
- Codeforces 293B Distinct Paths DFS+剪枝+状压
目录 题面 题目链接 题意翻译 输入输出样例 输入样例#1 输出样例#1 输入样例#2 输出样例#2 输入样例#3 输出样例#3 输入样例#4 输出样例#4 说明 思路 AC代码 总结 题面 题目链接 ...
- 【Gym100837F】Controlled Tournament(状压Dp 搜索剪枝)
题目链接 大意 现有\(N\)个人要打比赛,知道任意两个人间打比赛的胜负关系. 要求在 深度最小 的情况下,根为\(M\)的 竞赛树 的个数. 满足\(1\le M\le N\le 16\) 思路 虑 ...
- HDOJ 4770 Lights Against Dudely
状压+暴力搜索 Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 4770 Lights Against Dudely 暴力枚举+dfs
又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...
- hdu4770 Lights Against Dudely
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- hdu 4739 状压DP
这里有状态压缩DP的好博文 题目:题目比较神,自己看题目吧 分析: 大概有两种思路: 1.dfs,判断正方形的话可以通过枚举对角线,大概每次减少4个三角形,加上一些小剪枝的话可以过. 2.状压DP,先 ...
随机推荐
- javase(2)_递归&迭代
一.递归 程序调用自身的编程技巧称为递归( recursion).递归做为一种算法在程序设计语言中广泛应用. 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一个大型复杂的问题 ...
- Luogu P3727 曼哈顿计划E 点分治+hash
题目: P3727曼哈顿计划E 分析: 大长题面容易给人一种不可做的错觉,但是这题考的知识点都是我们熟悉的. 稍加分析我们可以得到,我们可以把每个点当成一个单独的游戏,如果k=1,就是简单的nim游戏 ...
- 【Java_多线程并发编程】JUC原子类——原子类中的volatile变量和CAS函数
JUC中的原子类是依靠volatile变量和Unsafe类中的CAS函数实现的. 1. volatile变量的特性 内存可见性(当一个线程修改volatile变量的值后,另一个线程就可以实时看到此变量 ...
- 编译openwrt_MT7688_hiwooya
参考链接: 无涯论坛地址: http://www.hi-wooya.com/forum.php openwrt官网地址:https://openwrt.org/zh-cn/doc/howto/buil ...
- Python-约瑟夫环
n个人(以编号0,1,2,3...n-1分别表示)围坐在一张圆桌周围.从编号为0的人开始报数1,数到m的那个人出列: 他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直到圆桌周围的 ...
- c++-string-1
解答注意: 我写的时候考虑了: 1) " my"(设置flag,为true时表示上一个是非空格字符) 2) "hello John"(最后不是空格结尾, ...
- 【实验吧】因缺思汀的绕过&&拐弯抹角&&Forms&&天网管理系统
<?php error_reporting(); if (!isset($_POST['uname']) || !isset($_POST['pwd'])) { echo '<form a ...
- 【LeetCode】Symmetric Tree(对称二叉树)
这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...
- nw335 debian sid x86-64 --3 linux内核自带
nw335 debian sid x86-64 --3 linux内核自带
- 学习Gulp过程中遇到的一些单词含义
注:以下有的单词的含义不仅仅在gulp里面是一样的,在其他某些语言里面也是一样 nodejs Doc:https://nodejs.org/api/stream.html gulp Api:http: ...