hdu 4770 Lights Against Dudely (2013亚洲区域赛杭州站 A)

来源:互联网 发布:600831广电网络 编辑:程序博客网 时间:2024/05/17 07:46

http://acm.hdu.edu.cn/showproblem.php?pid=4770


Lights Against Dudely

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


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. 

题目大意:一个n*m的房间有的房间是可破坏的,其他不可破坏,现在给你一些L型的灯,其中只有一个可以旋转,其他不能,让你用最少的灯覆盖所有的可破坏区域,且不能覆盖不可破坏的区域。


好久没有更新博客了,真是懒到家了,正好最近你做了下杭州站的题,就发几道简单题的题解吧。。。

思路:一开始没有看到最多只有15个可破坏区域以至于误入歧途,关键在于可覆盖区域很小,所以我们可以暴力枚举灯的位置,首先枚举可旋转的等的位置和方向,然后状压枚举其他灯的位置,最多就2^15种可能嘛,随便水水就过了。。。取最小值的灯数就行。

代码太丑勿喷。。。

#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>using namespace std;int getnum(int x){    int sum=0;    while(x)    {        sum+=x%2;        x/=2;    }    return sum;}int dir[4][2]={-1,0,0,1,1,0,0,-1};char map[210][210];int vis[210][210];int po[20][2];int num,n,m;int check(int x,int y){    if(x<1||y<1||x>n||y>m)    return 0;    return 1;}int isok(int flag,int limit){    int i;    for(i=0;i<num;i++)    {        int x=po[i][0],y=po[i][1];        if(vis[x][y]==limit)        continue;        vis[x][y]=0;    }    for(i=0;i<num;i++)    {        if((flag>>i)&1)        {            int x=po[i][0],y=po[i][1];            if(check(x-1,y)&&map[x-1][y]=='#')            return 0;            if(!vis[x-1][y])            vis[x-1][y]=1;            if(check(x,y+1)&&map[x][y+1]=='#')            return 0;            if(!vis[x][y+1])            vis[x][y+1]=1;            if(!vis[x][y])            vis[x][y]=1;        }    }    for(i=0;i<num;i++)    {        int x=po[i][0],y=po[i][1];        if(vis[x][y]==0)        return 0;    }    return 1;}int main(){  //  freopen("dd.txt","r",stdin);    while(scanf("%d%d",&n,&m)&&(n+m))    {        memset(vis,0,sizeof(vis));        int i,j,k;        for(i=1;i<=n;i++)        {            scanf("%s",map[i]+1);        }        num=0;        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)            {                if(map[i][j]=='.')                {                    po[num][0]=i;                    po[num++][1]=j;                }            }        }        if(num==0)        {            printf("0\n");            continue;        }        int limit=1<<num,sum=10000;        int ans=2100000000;        for(i=0;i<num;i++)//枚举位置        {            int x=po[i][0],y=po[i][1],tmp=0;            for(j=0;j<4;j++)//枚举方向            {                for(k=0;k<num;k++)                {                    int xx=po[k][0],yy=po[k][1];                    vis[xx][yy]=0;                }                if((check(dir[j][0]+x,dir[j][1]+y)&&map[dir[j][0]+x][dir[j][1]+y]=='#')||(check(dir[(j+1)%4][0]+x,dir[(j+1)%4][1]+y)&&map[dir[(j+1)%4][0]+x][dir[(j+1)%4][1]+y]=='#'))                continue;                sum++;                vis[dir[j][0]+x][dir[j][1]+y]=sum;                vis[dir[(j+1)%4][0]+x][dir[(j+1)%4][1]+y]=sum;                vis[x][y]=sum;                for(k=0;k<limit;k++)                {                    if((k>>i)&1)                    continue;                    if(isok(k,sum))                    {                        ans=min(ans,1+getnum(k));                    }                }            }        }        if(ans==2100000000)        printf("-1\n");        else        printf("%d\n",ans);    }    return 0;}