Fire Game

来源:互联网 发布:linux编程入门 编辑:程序博客网 时间:2024/06/05 04:59

K - Fire Game

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

题目大意:俩人玩游戏,烧草地(放火烧山,牢底坐穿啊)。最多可以有两个起点,火向四个方向蔓延需一秒(上下左右),起始时间为0秒。问你最少需要多少时间把草烧光。如果烧不光,输出-1,否则输出那个最小时间。

解题思路:其实就是有两个起点的宽度优先搜索,我刚开始用两个队列来进行宽搜,没处理好一直错,当然,应该也能做,就是比较麻烦吧。后来想了一想,发现其实就当一个起点来做就行,只是刚开始可能有两个节点入队列。因为火烧起来并不互相影响,当一个起点就能解决。

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<queue>using namespace std;#define INF 0x3f3f3f3fint T,N,M,cou;char grid[15][15];struct Point{int x,y;int times;}point[105];Point p1,p2;int ans;bool book[15][15];bool isValid(int x,int y){if(x<0 || x>=N || y<0 || y>=M || grid[x][y]!='#' || book[x][y])return false;return true;}int dir[8]={0,1,0,-1, 1,0,-1,0};queue<Point> que1;int BFS(){int counter=1,time=0;Point tp1,tp2;memset(book,false,sizeof(book));que1.push(p1); book[p1.x][p1.y]=true;if(p1.x!=p2.x || p1.y!=p2.y)//起点不同 {que1.push(p2); ++counter;book[p2.x][p2.y]=true;}while(!que1.empty()){if(!que1.empty()){tp1=que1.front();for(int i=0;i<8;i+=2){if(isValid(tp1.x+dir[i], tp1.y+dir[i+1])){tp2.times=tp1.times+1;tp2.x=tp1.x+dir[i];tp2.y=tp1.y+dir[i+1];que1.push(tp2);time=tp2.times;book[tp2.x][tp2.y]=true; ++counter;}}que1.pop();}}if(counter<cou)return -1;return time;}int cas;int main(){scanf("%d",&T);cas=1;while(T--){cou=0; ans=INF;scanf("%d%d",&N,&M);for(int i=0;i<N;i++)scanf("%s",grid[i]);for(int i=0;i<N;i++)for(int j=0;j<M;j++){if(grid[i][j]=='#'){point[cou].x=i; point[cou].y=j; point[cou++].times=0;}}for(int i=0;i<cou;i++){p1=point[i];for(int j=0;j<cou;j++){p2=point[j];int tans=BFS();if(tans!=-1)ans=min(ans,tans);}}if(ans==INF)ans=-1;printf("Case %d: %d\n",cas++,ans);}     return 0;    } 


1 0
原创粉丝点击