Fire Game

来源:互联网 发布:js object添加元素 编辑:程序博客网 时间:2024/06/06 11:58

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

这道题说白了,其实就是最短路径问题,果断用宽搜,但是我们这里要注意的是,这道题是两个起点的宽搜,两个同时搜,而不是分开搜,我本人就是因为这个错误而做了很久的

#include<iostream>#include<cstring>#include<queue>using namespace std;const int maxn=20;const int inf=0x3f3f3f3f;char a[maxn][maxn];//char b[maxn][maxn];int step[maxn][maxn];int usedtime=111111111;int direction[4][2]={{0,1},{1,0},{-1,0},{0,-1}};int column,line;int ans=0;int grasses; int cases=0;struct wo{int x;int y;};queue<wo>p;int fire(){int grass=0,time=0;while(p.size()){struct wo help;//结构体的入队需要结构体才能入队help=p.front();p.pop();
for(int i=0;i<4;i++){int x1=help.x+direction[i][0],y1=help.y+direction[i][1];if(x1<1||x1>line)continue;if(y1<1||y1>column)continue;if(a[x1][y1]=='.')continue;if(step[x1][y1]<=step[help.x][help.y]+1)continue;//满足条件才入队     step[x1][y1]=step[help.x][help.y]+1; wo help1; help1.x=x1;help1.y=y1; p.push(help1); grass++;//计算燃烧的草的个数 time=max(step[x1][y1],time);//算时间// cout<<x1<<" "<<y1<<" "<<step[x1][y1]<<endl;}} //cout<<time;if(grass==grasses-2)return time;//看燃烧完没有else return 111111111;}
int main(){ int t; cin>>t; while(t--){ usedtime=111111111,grasses=0; cin>>line>>column; for(int i=1;i<=line;i++){
for(int j=1;j<=column;j++){ cin>>a[i][j]; if(a[i][j]=='#')grasses++;//计算草的个数 } }  for(int i=1;i<=line;i++){ for(int j=1;j<=column;j++){ if(a[i][j]=='.')continue;//只从有草的地方开始燃烧 for(int s=1;s<=line;s++){ for(int l=1;l<=column;l++){ if(a[s][l]=='.')continue; if(l<=column){  while(p.size())p.pop();  if(i!=s||j!=l){  //cout<<a[i][j]<<" "<<a[s][l]<<endl; memset(step,inf,sizeof(step));         wo ll;         ll.x=i;ll.y=j;step[i][j]=0;         p.push(ll);         ll.x=s;ll.y=l;step[s][l]=0;         p.push(ll);         int ff=fire();         usedtime=min(usedtime,ff); 
} }  } }}}cout<<"Case"<<" "<<++cases<<": "; if(usedtime!=111111111) cout<<usedtime<<endl;  else cout<<-1<<endl; } }