现在的位置: 首页 > 综合 > 正文

hdu4770Lights Against Dudely

2013年02月13日 ⁄ 综合 ⁄ 共 5001字 ⁄ 字号 评论关闭

Lights Against Dudely

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

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
 

Recommend
We have carefully selected several similar problems for you:  4780 4779 4778 4777 4776 
 

Statistic | Submit | Discuss
| Note

题意:一幅地图里面有坚实的房间和虚弱的房间,你需要用灯来照亮虚弱的房间,

但是不能照到坚实的房间。有两种灯。问最小需要多少盏灯才能照亮全部的虚弱房间

刚开始还没什么想法。还一直在想要用到什么算法

但是后来一看数据量,才15个虚弱的房间。。

直接枚举才2^15*15种可能,绝对不会超时的

然后就想到状态压缩,对于每个虚弱的房间都有两种可能。一种是放灯,一种是不放灯。

也就是要用以为,把每一种状态都考虑一遍,再更新最小值。

#include<cstdio>
#include<cstring>
char map[205][205];
int dir[][2] = {{-1,0},{0,1},{1,0},{0,-1}};
struct node 
{
    int x,y;
}node[20];     //存地图中的‘.’的横纵坐标
int min(int a,int b)
{
    return a<b?a:b;
}
int main()
{
    int N,M,i,j;
    while(scanf("%d%d",&N,&M) && N+M!=0)
    {
        memset(map,'%',sizeof(map));  //将其余的状态定义为%,这样可以将map外面的和已经点亮过的‘.’做一样的对待
        int n=0;
        for(i=1;i<=N;i++)
        {
            getchar();
            for(j=1;j<=M;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='.')
                {
                    node[n].x = i;
                    node[n++].y = j;
                }
            }
        }
        
        if(n==0)
        {
            printf("0\n");
            continue;
        }
        int kk = 1<<n; //总共有n个虚弱的房间,就有2^n种状态
        int flag[20];   //flag[i]代表二进制中第i个数是0还是1,也就是放灯还是不放
        int Min = 100;
        for(i=1;i<kk;i++)
        {
            int nn = 0;
            for(j=0;j<n;j++)
            {
                flag[j] = 0;
                if(i&(1<<j))
                {
                    flag[j] = 1; //判断这个i状态下的那几个房间是要放灯
                    nn++;
                }
            }
            for(j=0;j<n;j++)  //j是代表可变的那盏灯是放在j处
            {
                int lig = 0;
                if(flag[j]==0)
                    continue;
                for(int q=0;q<n;q++)  //每一次遍历一次前都要把地图恢复
                {
                    map[node[q].x][node[q].y] = '.';
                }
                
                for(int k=0;k<n;k++)
                {                    
                    if(flag[k]==1 && k!=j)  //先把不能变化角度的灯全部放好
                    {
                        if(map[node[k].x-1][node[k].y]=='#' || map[node[k].x][node[k].y+1]=='#')
                            continue;
                        if(map[node[k].x][node[k].y]=='.')
                        {
                            map[node[k].x][node[k].y] = '%'; //要变成%对于以后的这种状态就不用lig++了
                            lig++;
                        }
                        if(map[node[k].x-1][node[k].y]=='.')
                        {
                            map[node[k].x-1][node[k].y] = '%';
                            lig++;
                        }
                        if(map[node[k].x][node[k].y+1]=='.')
                        {
                            map[node[k].x][node[k].y+1] = '%';
                            lig++;
                        }
                        
                    }
                }
                
                for(int r=0;r<4;r++)  //对于最后一种状态就有四种方法
                {
                    int ll = lig;
                    int one = r;   //每次转移方向都符合one = r;two = (r+1)%4;
                    int two = (r+1)%4;
                    if(map[node[j].x+dir[one][0]][node[j].y+dir[one][1]]=='#' || map[node[j].x+dir[two][0]][node[j].y+dir[two][1]]=='#')
                        continue;
                    if(map[node[j].x][node[j].y]=='.')
                    {
                        //map[node[j].x][node[j].y] = '%';  //上一次wa的原因是不记得这里是不能再更新了
                        ll++;
                    }
                    if(map[node[j].x+dir[one][0]][node[j].y+dir[one][1]]=='.')
                    {
                        //map[node[j].x+dir[one][0]][node[j].y+dir[one][1]] = '%';
                        ll++;
                    }
                    if(map[node[j].x+dir[two][0]][node[j].y+dir[two][1]]=='.')
                    {
                        //map[node[j].x+dir[two][0]][node[j].y+dir[two][1]] = '%';
                        ll++;
                    }
                    if(ll==n)
                        Min = min(Min,nn);
                    
                }
                
                
            }
        }

        if(Min!=100) 
            printf("%d\n",Min);
        else    //如果Min没有更新过,就是没有满足的情况
            printf("-1\n");
        
    }
    return 0;
    
}

抱歉!评论已关闭.