状压+暴力搜索

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. User mode Linux

    一.简介 用户模式Linux(User ModeLinux,UML)不同于其他Linux虚拟化项目,UML尽量将它自己作为一个普通的程序.从Linux2.6.9版本起,用户模式Linux(User m ...

  2. Shell命令和流程控制

    Shell命令和流程控制 在shell脚本中可以使用三类命令: 1)Unix 命令: 虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令.这些命令通常是用来进行文件和文字 ...

  3. Apache服务停止:信号灯超时时间已到,指定的网络名不再可用

    环境说明:Apache2.4.10,Windows Server 2008 R2 问题说明: apache服务用于下载文件,但是在运行一段时间后,突然挂了. 其错误提示如下所示: [error] (7 ...

  4. (一)java arcgis开发环境搭建

    一,整个开发环境 OS:Win7 Development: eclipse 4.3.2 框架:spring+springMVC+mybatis+jquery Arcgis版本:10.2 desktop ...

  5. .NET跨平台之旅:在Linux上将ASP.NET 5运行日志写入文件

    在前一篇博文(增加文件日志功能遇到的挫折)中,我们遇到了这样一个问题:虽然有一些.NET日志组件(比如Serilog, NLog)已经开始支持.NET Core,但目前只支持控制台输出日志,不支持将日 ...

  6. 工作邮件loop的用法

    examples come from native speaker Put john in the loop about this. He will have good advice. Why hav ...

  7. 关于#define for if(false);else for

    今日在看一个第三方代码时看到了#define for if(false);else for 这样的一种定义,不明白这样用法的目的,于是查了一下. 这是一个兼容vc6.0的用法,csdn上有这个问题的回 ...

  8. RTMP流媒体播放过程

      RTMP协议规定:第一步,建立一个网络连接(NetConnection):客户端和服务端的基础连通关系 第二步:建立一个网络流(NetStream)发送多媒体的通道(只能建立一个网络连接,可以建立 ...

  9. 使用AccessibilityService执行开机自启动

    res/xml/accessibility_service_config.xml <accessibility-service xmlns:android="http://schema ...

  10. Power BI REST API

    Overview of Power BI REST API https://msdn.microsoft.com/en-us/library/dn877544.aspx 验证方式 OAuth 2.0 ...