您的位置:首页 > 其它

2013杭州站A题||hdu4770 状态 压缩+暴力枚举

2014-09-02 11:57 323 查看
http://acm.hdu.edu.cn/showproblem.php?pid=4770

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

解题思路: 二进制压缩枚举所有可以放的情况。 灯光可以超出边界。只有不照到非法区域就可以。 
<pre name="code" class="cpp">#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
int n,m,h;
char map1[205][205];
int map2[205][205];
int point[25][2];
int sav[20];
int visi[25];//是否被照亮
int vis[25];//是否放灯
int dir[4][2]= {-1,0,0,1,1,0,0,-1};
int south[25],north[25],east[25],west[25];
int ok1(int p)//不带旋转的
{
int x1,y1,x2,y2,x,y;
x=point[p][0],y=point[p][1];
x1=x+dir[0][0],y1=y+dir[0][1];
x2=x+dir[1][0],y2=y+dir[1][1];
if(map1[x1][y1]=='.'&&map1[x2][y2]=='.')
return 1;
return 0;
}
int ok2(int p,int i)//特殊点
{
int x1,y1,x2,y2,x,y,j;
x=point[p][0],y=point[p][1];
j=(i+1)%4;
x1=x+dir[i][0],y1=y+dir[i][1];
x2=x+dir[j][0],y2=y+dir[j][1];
if(map1[x1][y1]=='.'&&map1[x2][y2]=='.')
return 1;
return 0;
}
int cover()
{
for(int i=0; i<h; i++)
if(!visi[i])
return 0;
return 1;
}
int solve()
{
int top=0;
for(int i=0; i<h; i++)
if(vis[i])
sav[top++]=i;
if(top*3<h)return 20;
for(int k=0; k<=3; k++)
{
int flag;
for(int i=0; i<top; i++)
{
if(!ok2(sav[i],k))continue;
int ii=sav[i];
memset(visi,0,sizeof(visi));
visi[ii]=1;
if(k==0)
visi[north[ii]]=1,visi[east[ii]]=1;
else if(k==1)
visi[south[ii]]=1,visi[east[ii]]=1;
else if(k==2)
visi[south[ii]]=1,visi[west[ii]]=1;
else
visi[north[ii]]=1,visi[west[ii]]=1;

flag=0;
for(int j=0; j<top; j++)
{
if(i==j)continue;
if(!ok1(sav[j]))//因为我们是在枚举,sav[j]点已经放上了,若判断出不能放,则这种状态不可以,继续枚举下一种状态
{
flag=1;
break;
}
int jj=sav[j];
visi[jj]=1;
visi[north[jj]]=1,visi[east[jj]]=1;
}
if(flag)
continue;
if(cover())
return top;
}
}
return 20;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(m==0&&n==0)
break;
for(int i=1; i<=n; i++)
scanf("%s",map1[i]+1);
h=0;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
if(map1[i][j]=='.')
{
map2[i][j]=h;
point[h][0]=i;
point[h++][1]=j;
}
else
map2[i][j]=20;
}
for(int i=0; i<=m+1; i++) //边界点设为‘.’
{
map1[0][i]='.',map1[n+1][i]='.';
map2[0][i]=20,map2[n+1][i]=20;
}
for(int i=0; i<=n+1; i++)
{
map1[i][0]='.',map1[i][m+1]='.';
map2[i][0]=20,map2[i][m+1]=20;
}
for(int i=0; i<h; i++)
{
south[i]=20;
north[i]=20;
east[i]=20;
west[i]=20;
}
int i,j;
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
{
if(map1[i][j]=='.')
{
if(map1[i][j-1]=='.')
{
west[map2[i][j]]=map2[i][j-1];
east[map2[i][j-1]]=map2[i][j];
}
if(map1[i][j+1]=='.')
{
east[map2[i][j]]=map2[i][j+1];
west[map2[i][j+1]]=map2[i][j];
}
if(map1[i-1][j]=='.')
{
north[map2[i][j]]=map2[i-1][j];
south[map2[i-1][j]]=map2[i][j];
}
if(map1[i+1][j]=='.')
{
south[map2[i][j]]=map2[i+1][j];
north[map2[i+1][j]]=map2[i][j];
}
}
}
int s=(1<<h)-1;
int res=20;
if(s==0)
{
puts("0");
continue;
}
for(i=1; i<=s; i++)
{
int tmp=i;
for(j=0; j<h; j++)
{
vis[j]=tmp&1;
tmp>>=1;
}

res=min(res,solve());
}
if(res==20)
{
puts("-1");
continue;
}
else
cout<<res<<endl;
}
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: