Fire Game

来源:互联网 发布:武汉js防水材料市场 编辑:程序博客网 时间:2024/06/06 07:36

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

题意:有一个n*m的网格状的平板,有的格子有干草,有的则为空,现在有两个人在玩点火游戏,他们可以各选择任意一个点点火,并且火会向四周蔓延(上下左右),每秒钟可以蔓延一次,问你能不能把所有草给点完,可以的话,求解最少花费时间,不可以的话输出-1。
模型:双起点的BFS。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;const int maxn = 100+10;const int INF = 0x3f3f3f3f;int T,n,m,ans,cnt,num,step,vist[maxn][maxn];char Map[maxn][maxn];struct node{    int x,y,cnt;}a[100100];int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};int BFS(node a,node b){    vist[a.x][a.y] = vist[b.x][b.y] = 1;    queue<node>Q;    Q.push(a);    Q.push(b);    node temp1,temp2;    int ttt = 0;    while(!Q.empty()){        temp1 = Q.front();        ttt = max(ttt,temp1.cnt);        Q.pop();        for(int i = 0; i < 4; i++){            temp2.x = temp1.x+dx[i];            temp2.y = temp1.y+dy[i];            temp2.cnt = temp1.cnt+1;            if(temp2.x<n && temp2.y<m && temp2.x>=0 && temp2.y>=0 && !vist[temp2.x][temp2.y] && Map[temp2.x][temp2.y] == '#'){                vist[temp2.x][temp2.y] = 1;                Q.push(temp2);            }        }    }    return ttt;}int main(){    scanf("%d",&T);    for(int t = 1; t <= T; t++){        ans = INF;        cnt = 0;        scanf("%d %d",&n,&m);        for(int i = 0; i < n; i++)            scanf("%s",Map[i]);        for(int i = 0; i < n; i++){            for(int j = 0; j < m; j++){                if(Map[i][j] == '#'){                    a[cnt].x = i;                    a[cnt].y = j;                    a[cnt++].cnt = 0;                }            }        }        printf("Case %d: ",t);        if(cnt == 0 || cnt == 1 || cnt == 2){            printf("0\n");            continue;        }        else{            for(int i = 0; i < cnt-1; i++)                for(int j = i+1; j < cnt; j++){                    memset(vist,0,sizeof(vist));                    step = BFS(a[i],a[j]);                    for(int i = 0; i < cnt; i++){                        if(!vist[a[i].x][a[i].y]){                            step = INF;                            break;                        }                    }                    ans = min(ans,step);                }            if(ans == INF)                printf("-1\n");            else                printf("%d\n",ans);        }    }    return 0;}
原创粉丝点击