Fire Game

来源:互联网 发布:网络在线咨询 编辑:程序博客网 时间:2024/06/12 20:53
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

43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#

Sample Output

Case 1: 1Case 2: -1Case 3: 0Case 4: 2

自己的思路当时局限在:
1.不知道怎样找这两个点,就是说没办法用程序来实现找到这两个点,时间最短,还能够烧完
2.没有办法判定是不是已经燃烧完了所有的草
解决思路:
1.利用枚举。先记录下,所有草的位置信息,然后利用枚举两个点,进行“燃烧”(广搜)。
2.每枚举两个点之后,我们都要再去找所有的草,看看是不是都已经被visit,只要有1个没有被visit,就说明刚才的两个起火点燃烧不完,如果能的话,我们就去选择最小的时间的。
3.还有就是这两个点同时燃烧的,不用管,直接全部压进队列就好。

#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;const int Max=11;char mp[Max][Max];int vis[Max][Max];int n,m;int next_move[4][2]={{0,1},{0,-1},{-1,0},{1,0}};typedef struct node{    int x,y;    int step;}Node;//记录草的位置Node grass[130];int no_way(int x,int y){    if(x<1||y<1||x>n||y>m||vis[x][y]||mp[x][y]=='.')        return 1;    return 0;}//直接把两个草的信息传递过来,因为同时燃烧,所以要同时模拟考虑int  bfs(Node a,Node b){    int Min_time=0x3f3f3f3f;//超级大的数    int max_time=-1;    queue<Node> q;    a.step=b.step=0;    memset(vis,0,sizeof(vis));    vis[a.x][a.y]=1,vis[b.x][b.y]=1;//标记已经燃烧    q.push(a);q.push(b);//现在感觉那个先烧都是一样的,毕竟都是一个范围是谁的无所谓    while(q.size())    {        a=q.front();        q.pop();        if(a.step>max_time)            max_time=a.step;//保留下最后一棵草的燃烧结束时间        for(int i=0;i<4;i++)        {            b.x=a.x+next_move[i][0];            b.y=a.y+next_move[i][1];            if(no_way(b.x,b.y))//这个地方不可以烧                continue;            vis[b.x][b.y]=1;//标记燃烧            b.step=a.step+1;            q.push(b);        }    }    return max_time;}int Judge(){    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)    {        if(mp[i][j]=='#'&&!vis[i][j])//一旦有是草,但是没有在那两点燃烧的范围内            return 0;    }    return 1;}int main (){    int T;    scanf("%d",&T);    int jishu=1;    while(T--)    {        int ans=-1;        int cnt=0;        int flag=0;        int Min_time=0x3f3f3f3f;//超级大的数        memset(mp,0,sizeof(mp));        scanf("%d %d",&n,&m);        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)        {            scanf(" %c",&mp[i][j]);            if(mp[i][j]=='#')            {                grass[cnt].x=i,grass[cnt++].y=j;            }        }        //任意找两个点进行广搜,之前并不知道我们应该怎样去尝试,是全都压进队列,还是怎样        for(int i=0;i<cnt;i++)            for(int j=i;j<cnt;j++)        {             ans=bfs(grass[i],grass[j]);             if(Judge())             {                 flag=1;                 if(ans<Min_time)                    Min_time=ans;             }        }        printf("Case %d: ",jishu++);        if(flag)            printf("%d\n",Min_time);        else            printf("-1\n");    }    return 0;}