状压+暴力搜索

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. java 静态内部类小总结

    内部类是指在一个外部类的内部再定义一个类.内部类作为外部类的一个成员,并且依附于外部类而存在的.内部类可为静态,可用protected和private修饰(而外部类只能使用public和缺省的包访问权 ...

  2. jquery each函数 break和continue功能

    jquery each函数 break和continue功能幸运的是另一个突破,持续一个jQuery循环方式.你可以打破在函数返回一个jQuery参数虚假循环.一个可以继续执行只是在做不指定返回值或返 ...

  3. Centos6下安装高版本Git

    yum remove git .tar.gz /usr/src/ cd /usr/src/ cd git-/ make configure whereis autoconf yum install a ...

  4. UOJ #221 【NOI2016】 循环之美

    题目链接:循环之美 这道题感觉非常优美--能有一个这么优美的题面和较高的思维难度真的不容易-- 为了表示方便,让我先讲一下两个符号.\([a]\)表示如果\(a\)为真,那么返回\(1\),否则返回\ ...

  5. [LeetCode] Remove Element 移除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  6. List [][]

    # -*- coding:utf-8 -*-   L = [     ['Apple', 'Google', 'Microsoft'],     ['Java', 'Python', 'Ruby', ...

  7. Quartz定时任务

    spring多个定时任务quartz配置 例子1: biz-quartz-context.xml配置 <?xml version="1.0" encoding="U ...

  8. 使用 win+r 命令行打开我们的桌面应用(处女座的福音)

    首先新建一个文件夹,名为quickapp,然后在地址栏复制文件目录地址,进入系统高级设置,修改系统环境变量Path,双击后选择新建,输入quickapp文件目录地址,确认保存. 如何修改path变量? ...

  9. mysql-You can’t specify target table for update in FROM clause错误

    mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...

  10. BZOJ 2001: [Hnoi2010]City 城市建设

    2001: [Hnoi2010]City 城市建设 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1132  Solved: 555[Submit][ ...