Fire Game (BFS)

来源:互联网 发布:淘宝用户年龄分布 编辑:程序博客网 时间:2024/04/30 01:18

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: 0

Case 4: 2

问题分析:两点的BFS,其实就是在地图中一次枚举两个点,一开始就将两个点燃的点入队列,然后按正常的BFS方式做,但是要让队列为空时停止,然后选取队列为空时所走步骤的最大值,因为只有在队列为空时才能说明一开始两个点燃的草坪已经不能再往外烧了,然后在判断一下是否所有草坪烧完了,如果烧完,再取最小值

#include<stdio.h>#include<string.h>#include<iostream>#include<queue>using namespace std;typedef struct node{int x,y;int time;}Q;const int inf = 99999999;int way[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};int book[15][15];char maze[15][15];int t,n,m;int minn;int cnt;//草坪数量 Q pos[150];//记录草坪的位置 int bfs(int a,int b){queue<Q>q;Q cur,next;int maxn = -1;cur.x = pos[a].x;cur.y = pos[a].y;cur.time = 0;q.push(cur);cur.x = pos[b].x;cur.y = pos[b].y;cur.time = 0;q.push(cur);book[pos[a].x][pos[a].y] = 1;book[pos[b].x][pos[b].y] = 1;while(!q.empty()){cur = q.front();q.pop();if (maxn<cur.time)maxn = cur.time;for(int i=0; i<4; i++){next.x = cur.x+way[i][0];next.y = cur.y+way[i][1];if (next.x<0 || next.y<0 || next.x>=n || next.y>=m)continue;if (book[next.x][next.y]==0 && maze[next.x][next.y]=='#'){next.time = cur.time+1;book[next.x][next.y] = 1;q.push(next);}}} return maxn;}int main(){scanf("%d",&t);for(int T=1; T<=t; T++){scanf("%d%d",&n,&m);cnt = 0;for(int i=0; i<n; i++){scanf("%s",maze[i]);for(int j=0; j<m; j++){if (maze[i][j]=='#') //计数草坪数量并记录位置 {cnt++;pos[cnt].x = i;pos[cnt].y = j;}}}printf("Case %d: ",T);if (cnt<=2){printf("0\n");continue;}minn = inf;int flag,tmp;for(int i=1; i<=cnt; i++){for(int j=i; j<=cnt; j++){flag = 1;memset(book,0,sizeof(book));tmp = bfs(i,j);//广搜完之后要判断是否全部烧完//检查是否全部烧尽 for(int k=0; k<n; k++){for(int l=0; l<m; l++){if (book[k][l]==0 && maze[k][l]=='#')//有没有烧的草坪 {flag = 0;break;}}if (flag==0)break;} if (flag)//草坪全部被烧,比较最小值 {if (minn > tmp){minn = tmp;}} }}if (minn==inf)printf("-1\n");elseprintf("%d\n",minn);}return 0;}


0 0