状压+暴力搜索

Lights Against Dudely

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 178    Accepted Submission(s): 57

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
 
Source
 
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int dir_x[]={-,,,},dir_y[]={,,,-}; int n,m,cnt=,ans;
char G[][];
bool den[][]; bool inside(int x,int y)
{
return (x>=&&x<n)&&(y>=&&y<m);
} void SHOWden()
{
for(int i=;i<n;i++,putchar())
for(int j=;j<m;j++)
cout<<den[i][j]<<" ";
cout<<" ... \n";
} void SHOWST(int x)
{
for(int i=;i<cnt;i++)
{
if(x&(<<i)) cout<<"1 ";
else cout<<"0 ";
}
cout<<endl;
} struct LP
{
int x,y;
}lp[]; void dfs(int state,int lan,int n)
{
//SHOWST(state);
//cout<<lan<<" ...... "<<n<<endl;
if(n>=ans) return ;
if(lan==cnt)
{
ans=n; return ;
}
for(int i=;i<cnt;i++)
{
if(state&(<<i)) continue;
int lan0=,lan1=,lan2=;
if(den[lp[i].x][lp[i].y]==)
{
lan0++; den[lp[i].x][lp[i].y]=;
}
int x1=lp[i].x+dir_x[],x2=lp[i].x+dir_x[];
int y1=lp[i].y+dir_y[],y2=lp[i].y+dir_y[];
if(!inside(x1,y1)||(inside(x1,y1)&&G[x1][y1]=='.'))
{
if(!inside(x2,y2)||(inside(x2,y2)&&G[x2][y2]=='.'))
{
if(inside(x1,y1)&&den[x1][y1]==)
{
lan1++; den[x1][y1]=;
}
if(inside(x2,y2)&&den[x2][y2]==)
{
lan2++; den[x2][y2]=;
}
dfs(state|(<<i),lan+lan2+lan0+lan1,n+);
}
}
if(lan0) den[lp[i].x][lp[i].y]=;
if(lan1) den[x1][y1]=;
if(lan2) den[x2][y2]=;
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
getchar();
memset(G,' ',sizeof(G));
cnt=;
for(int i=;i<n;i++)
{
scanf("%s",G[i]);
}
/*
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
cout<<" "<<G[i][j];
cout<<endl;
}
cout<<endl;
*/
memset(lp,,sizeof(lp));
memset(den,,sizeof(den));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(G[i][j]=='.')
{
lp[cnt].x=i;lp[cnt].y=j;
cnt++;
}
}
}
if(cnt==)
{
printf("0\n"); continue;
}
ans=0x3f3f3f3f;
for(int i=;i<cnt;i++)
{
den[lp[i].x][lp[i].y]=;
//cout<<lp[i].x<<" ..... "<<lp[i].y<<endl;
for(int j=;j<;j++)
{
int x1=lp[i].x+dir_x[j%],x2=lp[i].x+dir_x[(j+)%];
int y1=lp[i].y+dir_y[j%],y2=lp[i].y+dir_y[(j+)%];
if((inside(x1,y1)&&G[x1][y1]=='.')||!inside(x1,y1))
{
if((inside(x2,y2)&&G[x2][y2]=='.')||!inside(x2,y2))
{
int lan=;
if(inside(x1,y1)) { lan++; den[x1][y1]=; }
if(inside(x2,y2)) { lan++; den[x2][y2]=; }
//cout<<inside(x1,y1)<<" "<<x1<<","<<y1<<endl;
//cout<<inside(x2,y2)<<" "<<x2<<","<<y2<<endl;
//cout<<" .. \n";
//SHOWden();
dfs((<<i),lan,);
//SHOWden();
if(inside(x1,y1)) den[x1][y1]=;
if(inside(x2,y2)) den[x2][y2]=;
}
}
}
den[lp[i].x][lp[i].y]=;
}
if(ans==0x3f3f3f3f) puts("-1");
else printf("%d\n",ans);
}
return ;
}

HDOJ 4770 Lights Against Dudely的更多相关文章

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

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

  2. hdu 4770 Lights Against Dudely(回溯)

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

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

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

  4. HDU 4770 Lights Against Dudely

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

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

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

  6. HDU 4770 Lights Against Dudely(暴力+状压)

    思路: 这个题完全就是暴力的,就是代码长了一点. 用到了状压,因为之前不知道状压是个东西,大佬们天天说,可是我又没学过,所以对状压有一点阴影,不过这题中的状压还是蛮简单的. 枚举所有情况,取开灯数最少 ...

  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 Lights Against DudelyLights

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

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

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

随机推荐

  1. vim 学习积累(一)

    首先是简单的认识了三种状态(大家公认的说法是模式),分别是:插入,视图,和一般. 进入vim之后默认的是一般模式,这时直接使用'a', 'i', 'o'(也就是进入vim之后直接按下a/i/o键均可进 ...

  2. Intellij IDEA 一些不为人知的技巧

    Intellij IDEA 一些不为人知的技巧 2016/12/06 | 分类: 基础技术 | 0 条评论 | 标签: IntelliJ 分享到:38 原文出处: khotyn 今天又听了 Jetbr ...

  3. mac下openresty安装

    //openresty安装 http://openresty.org/ brew updatebrew install pcre openssl ./configure --prefix=/usr/l ...

  4. 使用Proj库进行大地坐标转空间坐标、投影坐标的一个示例

    最近研究了proj库的使用,自己写了一个小demo,仅供参考. void demoPROJ() { const char* wgs84 = "+proj=tmerc +ellps=WGS84 ...

  5. [LeetCode] Power of Four 判断4的次方数

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...

  6. sublimetext3中保存代码片段

    在日常的开发工作中,不断重复上一次敲过的代码,有时确实感到伐木累."蓝瘦"(难受)."香菇"(想哭),大概表达的也是这样的心境吧!:grinning: 所以,在 ...

  7. C++远征之封装篇(下)

    对象数组 类 x1[]; 栈中实例化,不用delete. 类 *X=new X[];//在堆中实例化,结尾需要用delete删除 delete []X; //这是数组的删除形式 X=NULL;

  8. Alpha阶段总结

    Alpha阶段的验收已经完成,8个小组都展现了他们经过连夜奋战后的成果.相比过往几届,这是第一次8个小组全部顺利演示操作完成,没有个别小组因为任务未完成而延宕演示的情况发生.Alpha演示,各组都实现 ...

  9. 使用css3做钟表

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 用上了ReSharper

    用上了ReSharper,发现很爽1.使用var2.去掉无用的using3.将foreach改为linq let4.字符串为cost5.作用域与变量名适配6.文件夹与namespace不匹配7.去掉为 ...