Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2653    Accepted Submission(s): 795


Problem Description
Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money." 

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. 

 

Input
  There are several test cases.

  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
 

Output
  For each test case, print the minimum number of lights which Dumbledore needs to put.

  If there are no vulnerable rooms, print 0.

  If Dumbledore has no way to light up all vulnerable rooms, print -1.
 

Sample Input

2 2
##
##
2 3
#..
..#
3 3
###
#.#
###
0 0
 

Sample Output

0
2
-1
 
这题可以用状压的思想做,因为题目中说要照明的点的个数不超过15个,所以可以枚举所有情况,复杂度为4*15*(2^15)*10,是可以接受的。先枚举特殊的灯以及其放的位置,然后枚举其余灯放其他点的状态,看是否符合。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x7fffffff
#define maxn 205
char gra[maxn][maxn],lit[maxn][maxn],vis[maxn][maxn];
int a[30][5],c[30];
int tot; int check()
{
int i,j,x,y;
int flag=1;
for(i=1;i<=tot;i++){
x=a[i][0];
y=a[i][1];
if(lit[x][y])continue;
else flag=0;
}
return flag;
} int main()
{
int n,m,i,j,h,k;
int x1,x2,x3,y1,y2,y3,x,y,now,xx1,xx2,yy1,yy2,xx,yy,s,zt;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0 && m==0)break;
tot=0;
for(i=1;i<=n;i++){
scanf("%s",gra[i]+1);
for(j=1;j<=m;j++){
if(gra[i][j]=='.'){
tot++;
a[tot][0]=i;
a[tot][1]=j;
a[tot][2]=(1<<(tot-1) );
}
} }
if(tot==0){
printf("0\n");continue;
}
int ans=inf;
for(h=1;h<=4;h++){
for(k=1;k<=tot;k++){
for(i=1;i<=tot;i++){
vis[a[i][0] ][a[i][1] ]=0;
lit[a[i][0] ][a[i][1] ]=0;
}
x=a[k][0];
y=a[k][1];
if(h==1){
x1=x-1; y1=y;
x2=x; y2=y+1;
}
else if(h==2){
x1=x+1; y1=y;
x2=x; y2=y+1;
}
else if(h==3){
x1=x+1; y1=y;
x2=x; y2=y-1; }
else if(h==4){
x1=x-1; y1=y;
x2=x; y2=y-1 ;
} if(x1>=1 && x1<=n && y1>=1 && y1<=m){
if(gra[x1][y1]=='#')continue;
}
if(x2>=1 && x2<=n && y2>=1 && y2<=m){
if(gra[x2][y2]=='#')continue;
}
vis[x][y]=1;lit[x][y]=1;
if(x1>=1 && x1<=n && y1>=1 && y1<=m){
lit[x1][y1]=1;
}
if(x2>=1 && x2<=n && y2>=1 && y2<=m){
lit[x2][y2]=1;
} if(check()){
ans=min(ans,1);break;
} for(zt=1;zt<=(1<<tot)-1;zt++){
int s=zt;
for(i=1;i<=tot;i++){
vis[a[i][0] ][a[i][1] ]=0;
lit[a[i][0] ][a[i][1] ]=0; }
if(x1>=1 && x1<=n && y1>=1 && y1<=m){
if(gra[x1][y1]=='#')continue;
}
if(x2>=1 && x2<=n && y2>=1 && y2<=m){
if(gra[x2][y2]=='#')continue;
}
vis[x][y]=1;lit[x][y]=1;
if(x1>=1 && x1<=n && y1>=1 && y1<=m){
lit[x1][y1]=1;
}
if(x2>=1 && x2<=n && y2>=1 && y2<=m){
lit[x2][y2]=1;
}
if(s&(a[k][2] ))continue;
int wei=0;
int geshu=0;
while(s)
{
wei++;
c[wei]=s%2;
if(c[wei]==1)geshu++;
s/=2;
}
if(geshu+1>ans)continue;
int flag1=1;
for(i=1;i<=wei;i++){
if(c[i]==1){
xx=a[i][0];
yy=a[i][1];
xx1=xx-1;yy1=yy;
xx2=xx;yy2=yy+1;
if(xx1>=1 && xx1<=n && yy1>=1 && yy1<=m){
if(gra[xx1][yy1]=='#'){
flag1=0;break;
}
}
if(xx2>=1 && xx2<=n && yy2>=1 && yy2<=m){
if(gra[xx2][yy2]=='#'){
flag1=0;break;
}
}
vis[xx][yy]=1;lit[xx][yy]=1;
if(xx1>=1 && xx1<=n && yy1>=1 && yy1<=m){
lit[xx1][yy1]=1;
}
if(xx2>=1 && xx2<=n && yy2>=1 && yy2<=m){
lit[xx2][yy2]=1;
}
}
}
if(flag1){
if(check()){
ans=min(ans,geshu+1);
}
}
} }
if(ans==1)break;
}
if(ans==inf)printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}



hdu4770 Lights Against Dudely的更多相关文章

  1. hdu4770:Lights Against Dudely(回溯 + 修剪)

    称号:hdu4770:Lights Against Dudely 题目大意:相同是n*m的矩阵代表room,房间相同也有脆弱和牢固之分,如今要求要保护脆弱的房间.须要将每一个脆弱的房间都照亮,可是牢固 ...

  2. HDOJ 4770 Lights Against Dudely

    状压+暴力搜索 Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. HDU 4770 Lights Against Dudely

    Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)

    Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. hdu 4770 Lights Against Dudely(回溯)

    pid=4770" target="_blank" style="">题目链接:hdu 4770 Lights Against Dudely 题 ...

  6. HDU 4770 Lights Against Dudely 暴力枚举+dfs

    又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...

  7. HDU_4770 Lights Against Dudely 状压+剪枝

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 Lights Against Dudely Time Limit: 2000/1000 MS ( ...

  8. hdu 4770 13 杭州 现场 A - Lights Against Dudely 暴力 bfs 状态压缩DP 难度:1

    Description Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money.&quo ...

  9. 状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely

    题目传送门 题意:有n*m的房间,'.'表示可以被点亮,'#'表示不能被点亮,每点亮一个房间会使旁边的房间也点亮,有意盏特别的灯可以选择周围不同方向的房间点亮.问最少需要多少灯使得所有房间点亮 分析: ...

随机推荐

  1. IPC 经典问题:Sleeping Barber Problem

    完整代码实现: #include <stdio.h> #include <unistd.h> #include <time.h> #include <stdl ...

  2. 十四:SQL注入之类型及提交注入

    简要明确参数类型 数字,字符,搜索,json等 简要明确请求方法 GET,POST,COOKIE,REQUEST,HTTP头 其中SQL语句干扰符号:' " % ) } 等,具体查看用法 非 ...

  3. C语言------三目运算符(条件运算符)

    今天在看C语言的时候看到了下面的代码(废话少说,直接上代码): #include <stdio.h> int main() {int max(); extern int A,B,C; // ...

  4. Docker 如何动态修改容器端口映射

    Docker端口映射往往是Docker Run命令时通过-p将容器内部端口映射到宿主机的指定端口上,一般来说容器的端口所对应的端口是提前确定需要映射的.但是有些情况下不得不需要临时映射端口,例如Doc ...

  5. 浅析鸿蒙中的 Gn 与 Ninja(一)

    目录: Ninja简介 make 的 3 个特性 举例说明Ninja 的用法 如何向构建工具 Ninja 描述构建图 后记 鸿蒙系统的编译构建是基于 Gn 和 Ninja 完成的,那么 Gn 和 Ni ...

  6. 自动化接口差异测试-diffy 回归测试 charles rewrite 请求

    https://mp.weixin.qq.com/s/vIxbtQtRRqgYCrDy7XTcrA 自动化接口差异测试-diffy Boris 搜狗测试 2018-08-30   自动化接口差异测试- ...

  7. mdns

    mdns mdns_百度百科 https://baike.baidu.com/item/mdns 在计算机网络中 , 多播DNS ( mDNS )协议将主机名解析为不包含本地名称服务器的小型网络中的I ...

  8. 关于notepad++不能设置中文的解决方法

    好久没用notepad++了,最近遇到一个base64的音频文件,想起来notepad++挺好用的 于是安装好 然后发现 最新的notepad++竟然不能显示中文界面 这怎么可以呢! 上网搜 果然 作 ...

  9. JAD 反编译

    自动拆装箱 对于基本类型和包装类型之间的转换,通过xxxValue()和valueOf()两个方法完成自动拆装箱,使用jad进行反编译可以看到该过程: public class Demo { publ ...

  10. UVA11694 Gokigen Naname题解

    目录 写在前面 Solution Code 写在前面 UVA的题需要自己读入一个 \(T\) 组数据,别被样例给迷惑了 Solution 每个格子只有两种填法且 \(n \le 7\),暴力搜索两种填 ...