BNUOJ 33992 Lights Against Dudely(dfs)

来源:互联网 发布:打车软件司机版 编辑:程序博客网 时间:2024/06/05 04:24

Lights Against Dudely

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 4770
64-bit integer IO format: %I64d      Java class name: Main
Prev 
Submit Status Statistics Discuss
 Next
Type: 
None
     
    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

    直接dfs暴力,先放入特殊灯,然后暴力放普通灯的图书情况,刷新最小值即可,感觉平时碰到这种题老是不敢暴力,考虑着图的变化,事实上,只要把返回搞好,暴力很好用
    #include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#define wtf printf("wtf\n");#define mem(a,b) memset(a,b,sizeof(a))#define maxn 210using namespace std;int n,m,len,ans;char h[maxn][maxn];bool sign[maxn][maxn];int mv[4][2][2];pair<int,int>p[20];void init(){       len=0,ans=0x3f3f3f3f;       for(int i=0;i<=n+1;i++)       {           for(int j=0;j<=m+1;j++)           {               h[i][j]='.';           }       }       mem(sign,false);}bool judge(){    for(int i=0;i<len;i++)    {        if(h[p[i].first][p[i].second]=='.')            return false;    }    return true;}void dfs(int st,int sum){    if(sum>ans) return;    if(st==len)    {        if(judge())        {            ans=min(ans,sum);        }        return;    }    char a,b,c;    int ax,ay,bx,by;    ax=p[st].first+mv[0][0][0];    ay=p[st].second+mv[0][0][1];    bx=p[st].first+mv[0][1][0];    by=p[st].second+mv[0][1][1];    a=h[ax][ay];    b=h[bx][by];    c=h[p[st].first][p[st].second];    if((a=='.'||sign[ax][ay]||ax>n||ax<1||ay>m||ay<1)&&(b=='.'||sign[bx][by]||bx>n||bx<1||by>m||by<1))    {        h[ax][ay]='#';        h[bx][by]='#';        h[p[st].first][p[st].second]='#';        dfs(st+1,sum+1);        h[ax][ay]=a;        h[bx][by]=b;        h[p[st].first][p[st].second]=c;    }    dfs(st+1,sum);}void _dfs(int x,int y){    char t1,t2;    int ax,ay,bx,by;    for(int i=0;i<4;i++)    {        ax=x+mv[i][0][0];        ay=y+mv[i][0][1];        bx=x+mv[i][1][0];        by=y+mv[i][1][1];        t1=h[ax][ay];        t2=h[bx][by];        if(t1=='#')continue;        if(t2=='#')continue;        h[ax][ay]='#';        h[bx][by]='#';        h[x][y]='#';        dfs(0,1);        h[ax][ay]=t1;        h[bx][by]=t2;        h[x][y]='.';    }}int main(){    mv[0][0][0]=-1,mv[0][0][1]=0,mv[0][1][0]=0,mv[0][1][1]=1;    mv[1][0][0]=0,mv[1][0][1]=1,mv[1][1][0]=1,mv[1][1][1]=0;    mv[2][0][0]=1,mv[2][0][1]=0,mv[2][1][0]=0,mv[2][1][1]=-1;    mv[3][0][0]=0,mv[3][0][1]=-1,mv[3][1][0]=-1,mv[3][1][1]=0;   while(~scanf("%d%d",&n,&m)&&n+m)   {         init();       for(int i=1;i<=n;i++)       {           scanf("%s",h[i]+1);       }       for(int i=1;i<=n;i++)       {           for(int j=1;j<=m;j++)           {               if(h[i][j]=='.')               {                   p[len].first=i;                   p[len].second=j;                   sign[i][j]=true;                   len++;               }           }       }       for(int i=0;i<len;i++)       _dfs(p[i].first,p[i].second);       if(len==0) ans=0;       if(ans==0x3f3f3f3f)ans=-1;       printf("%d\n",ans);   }   return 0;}




    0 0
    原创粉丝点击