Fire Game FZU - 2150

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

Output

For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.

Sample Input
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2
题意:有2把火,#代表草,问烧光所有草至少需要多少时间
Tip: 这题看过别人的思路。把2个点同时放入队列进行bfs,求每次step最大的最小值。这题需要注意的有在草数量小于
等于2个的时候应该输出0,WA了一次,我也是直觉告诉我应该测一下只有一个草的情况。就AC了。这题的地图比较小,所以
不会超时。
#include <stdio.h>
#include <string.h>
char map1[20][20];
char map2[20][20];
int temp[205];
int que[500];
int step[11][11];
int direct[4][2]={1,0,-1,0,0,1,0,-1};
int flag=0;
int min=10000;
int n,m;
int res;
int judge()  //如果有还有草,就返回0
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(map2[i][j]=='#')
            return 0;
        }
    }
    return 1;
}
void bfs(int sx1,int sy1,int sx2,int sy2)
{
    int T=-1000;
    int front=0,end=0;
    que[end++]=sx1,que[end++]=sy1,que[end++]=sx2,que[end++]=sy2;
    memset(step,0,sizeof(step));
    step[sx1][sy1]=0,step[sx2][sy2]=0;
    map2[sx1][sy1]='.' , map2[sx2][sy2]='.';
    while(front<end)
    {
        int x=que[front++];
        int y=que[front++];
        for(int i=0;i<4;i++)
        {
            int nx=x+direct[i][0];
            int ny=y+direct[i][1];
            if(nx>=0&&nx<=n-1&&ny>=0&&ny<=m-1&&step[nx][ny]==0&&map2[nx][ny]=='#')
            {
                map2[nx][ny]='.';
                que[end++]=nx;
                que[end++]=ny;
                step[nx][ny]=step[x][y]+1;
                if(step[nx][ny]>T)
                    T=step[nx][ny];
            }
        }
    }
    if(judge()==1)
    {
        flag=1;
        if(T<min)
            min=T;
    }
}
int main(void)
{
    int t;
    scanf("%d",&t);
    for(int o=1;o<=t;o++)
    {
        scanf("%d%d",&n,&m);
        flag=0;
        int cnt=0;
        for(int i=0;i<n;i++)
            scanf("%s",map1[i]);
        int index=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(map1[i][j]=='#')
                {
                    temp[index++]=i;
                    temp[index++]=j;
                    cnt++;
                }
            }
        }
        if(cnt<=2)
        {
            printf("Case %d: 0\n",o);
            continue;
        }
        min=100;
    //printf("%d\n %d",cnt,index);
        for(int i=0;i<=index-1;i+=2)
        {
            int sx1=temp[i];
            int sy1=temp[i+1];
            for(int j=i;j<=index-1;j+=2)
            {
                for(int k=0;k<n;k++)
                    for(int l=0;l<m;l++)
                        map2[k][l]=map1[k][l];
                int sx2=temp[j];
                int sy2=temp[j+1];
            //printf("%d %d %d %d \n",sx1,sy1,sx2,sy2);
            //if(sx1==sx2 && sy1==sy2)
                bfs(sx1,sy1,sx2,sy2);
            //else
            //    bfs(sx1,sy1,sx2,sy2,cnt-2);
            }
        }
        if(flag==1)
            printf("Case %d: %d\n",o,min);
        else
            printf("Case %d: -1\n",o);
        }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务