码迷,mamicode.com
首页 > 其他好文 > 详细

Lights Against Dudely

时间:2014-11-30 15:24:58      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

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:       
bubuko.com,布布扣
  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
 
源代码(Time Limit Exceeded):
 
 
#include<stdio.h>
char map[2100][2100];
int N,M;
int min;
struct weak
{
    int x,y;
}point[2100];
int sign;
void make_sign()
{
    int i,j;
    for(i=0;i<N;i++)
        for(j=0;j<M;j++)
            if(map[i][j]==.){
                point[sign].x=i;
                point[sign++].y=j;
            }
}
int seach_all()
{
    for(int i=0;i<N;i++)
        for(int j=0;j<M;j++)
            if(map[i][j]==.)
                return 0;
    return 1;
}
void light1(int x,int y)
{
    map[x][y]=1;
    if(x-1>=0)
        map[x-1][y]=1;
    if(y+1<M)
        map[x][y+1]=1;
}
void light2(int x,int y)
{
    map[x][y]=1;
    if(x+1<N)
        map[x+1][y]=1;
    if(y+1<M)
        map[x][y+1]=1;
}
void light3(int x,int y)
{
    map[x][y]=1;
    if(x+1<N)
        map[x+1][y]=1;
    if(y-1>=0)
        map[x][y-1]=1;
}
void light4(int x,int y)
{
    map[x][y]=1;
    if(x-1>=0)
        map[x-1][y]=1;
    if(y-1>=0)
        map[x][y-1]=1;
}
void DFS(int pt,int step)
{
    if(pt>=sign)
        return;
    if(seach_all()){
        if(min>step)
            min=step;
        return;
    }
    int i;
    char flag[5];
    for(i=0;i<=4;i++){
        if(i==0)
            DFS(pt+1,step);
        if(i==1&&(map[point[pt].x-1][point[pt].y]!=#||map[point[pt].x][point[pt].y+1]!=#)){
            flag[1]=map[point[pt].x][point[pt].y];
            flag[2]=map[point[pt].x-1][point[pt].y];
            flag[3]=map[point[pt].x][point[pt].y+1];
            light1(point[pt].x,point[pt].y);
            DFS(pt+1,step+1);
            map[point[pt].x][point[pt].y]=flag[1];
            map[point[pt].x-1][point[pt].y]=flag[2];
            map[point[pt].x][point[pt].y+1]=flag[3];
        }
        if(i==2&&(map[point[pt].x+1][point[pt].y]!=#||map[point[pt].x][point[pt].y+1]!=#)){
            flag[1]=map[point[pt].x][point[pt].y];
            flag[2]=map[point[pt].x+1][point[pt].y];
            flag[3]=map[point[pt].x][point[pt].y+1];
            light2(point[pt].x,point[pt].y);
            DFS(pt+1,step+1);
            map[point[pt].x][point[pt].y]=flag[1];
            map[point[pt].x+1][point[pt].y]=flag[2];
            map[point[pt].x][point[pt].y+1]=flag[3];
        }
        if(i==3&&(map[point[pt].x+1][point[pt].y]!=#||map[point[pt].x][point[pt].y-1]!=#)){
            flag[1]=map[point[pt].x][point[pt].y];
            flag[2]=map[point[pt].x+1][point[pt].y];
            flag[3]=map[point[pt].x][point[pt].y-1];
            light3(point[pt].x,point[pt].y);
            DFS(pt+1,step+1);
            map[point[pt].x][point[pt].y]=flag[1];
            map[point[pt].x+1][point[pt].y]=flag[2];
            map[point[pt].x][point[pt].y-1]=flag[3];
        }
        if(i==4&&(map[point[pt].x-1][point[pt].y]!=#||map[point[pt].x][point[pt].y-1]!=#)){
            flag[1]=map[point[pt].x][point[pt].y];
            flag[2]=map[point[pt].x-1][point[pt].y];
            flag[3]=map[point[pt].x][point[pt].y-1];
            light4(point[pt].x,point[pt].y);
            DFS(pt+1,step+1);
            map[point[pt].x][point[pt].y]=flag[1];
            map[point[pt].x-1][point[pt].y]=flag[2];
            map[point[pt].x][point[pt].y-1]=flag[3];
        }
    }
}
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF){
        if(N==0&&M==0)
            return 0;
        min=9999999;
        for(int i=0;i<N;i++)
            scanf("%s",map[i]);
        sign=0;
        make_sign();
        if(sign)
            DFS(0,0);
        if(min==9999999)
            min=-1;
        printf("%d\n",min);
    }
    return 0;
}

 

Lights Against Dudely

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/yanglingwell/p/4133088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!