HDU 4770 Lights Against Dudely(列舉所有狀態 當然壯壓dp會很簡單)

果7發表於2013-11-13

Lights Against Dudely

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


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
 


題目大意:點表示可以照亮的燈,普通的一盞燈可以照亮自己的位置和自己上和右的位置。特殊的燈可以照亮的位置可以將普通燈旋轉。問你最少使用多少燈照亮所有的點。
注意:
1.每次最多有一個特殊燈。
2.總共最多是15個點。
3.一個點如果被一盞燈佔住了,則不可以放燈,但是被照亮了,可以繼續放燈。

解題思路:當時開始討論的思路是暴利,時間複雜度,O(2^14*15*4)大概是10^6,然後可以加一個剪枝,top*3<h可以直接跳出。但最後是用壯壓dp寫出來的,直接0ms一A

題目地址:Lights Against Dudely


下面是自己的挫程式碼。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,h;    //h是點的個數
char map1[205][205];  //地圖
int dian[20][2];   //點的座標
int dir[4][2]= {{-1,0},{0,1},{1,0},{0,-1}}; //四個方向
int vis[20];
int sav[20];
int visi[25];  //是否全部點都被照亮
int map2[205][205];   //存放每個點的順序
int north[25],south[25],west[25],east[25];   //記錄每個點的上下左右點的在點裡的位置

int ok1(int p)   //只能右上
{
    int x1,y1,x2,y2,x,y;
    x=dian[p][0],y=dian[p][1];
    x1=x+dir[0][0],y1=y+dir[0][1];
    x2=x+dir[1][0],y2=y+dir[1][1];
    if(map1[x1][y1]=='.'&&map1[x2][y2]=='.')
        return 1;
    return 0;
}

int ok2(int p,int i)   //特殊點
{
    int x1,y1,x2,y2,x,y,j;
    x=dian[p][0],y=dian[p][1];
    j=(i+1)%4;
    x1=x+dir[i][0],y1=y+dir[i][1];
    x2=x+dir[j][0],y2=y+dir[j][1];
    if(map1[x1][y1]=='.'&&map1[x2][y2]=='.') return 1;
    return 0;
}

int cover()
{
    int i;
    for(i=0; i<h; i++)
        if(!visi[i])
            return 0;
    return 1;
}

int solve()
{
    int i,j,k;
    int top=0;
    for(i=0; i<h; i++)
        if(vis[i])
            sav[top++]=i;
    if(top*3<h) return 20;  //不可能照亮
    for(k=0; k<=3; k++)
    {
        int flag;
        for(i=0; i<top; i++)
        {
            if(!ok2(sav[i],k)) continue;
            int ii=sav[i];
            memset(visi,0,sizeof(visi));
            visi[ii]=1;
            if(k==0) visi[north[ii]]=1,visi[east[ii]]=1;
            else if(k==1) visi[south[ii]]=1,visi[east[ii]]=1;
            else if(k==2) visi[south[ii]]=1,visi[west[ii]]=1;
            else visi[north[ii]]=1,visi[west[ii]]=1;

            flag=0;
            for(j=0; j<top; j++)
            {
                if(i==j) continue;
                if(!ok1(sav[j]))
                {
                    flag=1;
                    break;
                }
                int jj=sav[j];
                visi[jj]=1;
                visi[north[jj]]=1,visi[east[jj]]=1;
            }

            if(flag) continue;
            if(cover()) return top;
        }
    }
    return 20;
}

int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        for(i=1; i<=n; i++)
            scanf("%s",map1[i]+1);

        h=0;   //點的個數
        for(i=1; i<=n; i++)
            for(j=1; j<=m; j++)
            {
                if(map1[i][j]=='.')
                {
                    map2[i][j]=h;
                    dian[h][0]=i;
                    dian[h++][1]=j;
                }
                else map2[i][j]=20;
            }

        for(i=0;i<h;i++)
            north[i]=south[i]=west[i]=east[i]=20;
        for(i=0; i<=m+1; i++)
        {
            map1[0][i]='.',map1[n+1][i]='.';
            map2[0][i]=20,map2[n+1][i]=20;
        }
        for(i=0; i<=n+1; i++)
        {
            map1[i][0]='.',map1[i][m+1]='.';
            map2[i][0]=20,map2[i][m+1]=20;
        }
        
        for(i=1; i<=n; i++)
            for(j=1; j<=m; j++)
            {
                if(map1[i][j]=='.')
                {
                    if(map1[i][j-1]=='.')
                    {
                        west[map2[i][j]]=map2[i][j-1];
                        east[map2[i][j-1]]=map2[i][j];
                    }
                    if(map1[i][j+1]=='.')
                    {
                        east[map2[i][j]]=map2[i][j+1];
                        west[map2[i][j+1]]=map2[i][j];
                    }
                    if(map1[i-1][j]=='.')
                    {
                        north[map2[i][j]]=map2[i-1][j];
                        south[map2[i-1][j]]=map2[i][j];
                    }
                    if(map1[i+1][j]=='.')
                    {
                        south[map2[i][j]]=map2[i+1][j];
                        north[map2[i+1][j]]=map2[i][j];
                    }
                }
            }
            
        int s=(1<<h)-1;
        int res=20;
        if(s==0)
        {
            puts("0");
            continue;
        }

        for(i=1; i<=s; i++)
        {
            int tmp=i;
            for(j=0; j<h; j++)
            {
                vis[j]=tmp&1;
                tmp>>=1;
            }

            res=min(res,solve());
        }
        if(res==20) { puts("-1"); continue;}
        else   cout<<res<<endl;
    }
    return 0;
}

/*
2 2
##
##
2 3
#..
..#
3 3
###
#.#
###
4 4
.##.
####
####
.##.
4 4
.##.
..#.
..#.
..#.
5 4
#.##
..#.
#.#.
#..#
....
2 4
.##.
####
2 4
.###
####
0
2
-1
-1
5
-1
2
1
*/

//93MS


相關文章