hdu 4770 Lights Against Dudely (2013亞洲區域賽杭州站 A)

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章