hdu 4770 Lights Against Dudely

来源:互联网 发布:软件代理商协议 编辑:程序博客网 时间:2024/05/16 08:10

Lights Against Dudely

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


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
02-1
 

Source
2013 Asia Hangzhou Regional Contest
 
题意:有不超过15个的空房间,让你在空房间里点灯,点灯之后能把(x,y),(x-1,y),(x,y+1)点亮,不过有一盏灯可以旋转90.180.270度,问你要至少点亮多少盏灯才能把空房间照亮,注意不能照亮不能用的房间.

做法:用两进制暴力枚举,先用(1<<n)枚举所有情况,然后用(1-n)枚举那盏灯作为特殊的灯,然后(0-3)枚举四个方向,最后取出最小值就可。

#include <iostream>#include <cstdio>#include <climits>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <map>#include <stack>#include <set>#include <algorithm>#include<ctime>#define esp 1e-6#define LL long long#define inf 0x0f0f0f0fusing namespace std;char mm[225][225];pair<int,int>pp[20];int n,m;int ans;int v[225][225];void solve(int nn,int ss){    int i,j,tt,a,b,ii;    tt=0;    int flag;    int ms,mi,ff,fx;    for(i=0;i<nn;i++)    {        if(ss&(1<<i))        {           for(fx=0;fx<4;fx++)           {            ff=0;            for(ii=0;ii<nn;ii++)            v[pp[ii].first][pp[ii].second]=1;            for(j=0;j<nn;j++)            {                flag=0;                if(ss&(1<<j))                {                ff++;                   if(j!=i)                   {                        a=pp[j].first;                        b=pp[j].second;                        if(mm[a-1][b]=='.'&&mm[a][b+1]=='.')                        {                            v[a][b]=0;                            v[a-1][b]=0;                            v[a][b+1]=0;                        }                        else                            flag=1;                        if(flag==1)                          break;                   }                }            }            if(flag==1)                continue;            flag=1;            if(fx==0)            {                a=pp[i].first;                        b=pp[i].second;                        if(mm[a-1][b]=='.'&&mm[a][b+1]=='.')                        {                            v[a][b]=0;                            v[a-1][b]=0;                            v[a][b+1]=0;                            flag=0;                        }            }            if(fx==1)            {                a=pp[i].first;                        b=pp[i].second;                        if(mm[a-1][b]=='.'&&mm[a][b-1]=='.')                        {                            v[a][b]=0;                            v[a-1][b]=0;                            v[a][b-1]=0;                            flag=0;                        }            }            if(fx==2)            {                a=pp[i].first;                        b=pp[i].second;                        if(mm[a+1][b]=='.'&&mm[a][b-1]=='.')                        {                            v[a][b]=0;                            v[a+1][b]=0;                            v[a][b-1]=0;                            flag=0;                        }            }            if(fx==3)            {                a=pp[i].first;                        b=pp[i].second;                        if(mm[a+1][b]=='.'&&mm[a][b+1]=='.')                        {                            v[a][b]=0;                            v[a+1][b]=0;                            v[a][b+1]=0;                            flag=0;                        }            }            int flag1;            flag1=0;            for(ii=0;ii<nn;ii++)                if(v[pp[ii].first][pp[ii].second]==1)                {                    flag1=1;                    break;                }            if(flag1==0&&flag==0)                ans=min(ans,ff);           }        }    }}int main(){   int ss;   int i,j;   while(scanf("%d%d",&n,&m)!=EOF)   {       ss=0;       if(n==0&&m==0)       break;       ans=inf;       memset(mm,'.',sizeof(mm));       for(i=1;i<=n;i++)       {           getchar();           for(j=1;j<=m;j++)           {           scanf("%c",&mm[i][j]);           if(mm[i][j]=='.')              {                  pp[ss++]=make_pair(i,j);              }           }       }       if(ss==0)       {           printf("0\n");       }       else       {           for(i=0;i<(1<<ss);i++)            solve(ss,i);           if(ans!=inf)           printf("%d\n",ans);           else            printf("-1\n");       }   }}


0 0