Description
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
4 3 3 .#. ### .#. 3 3 .#. #.# .#. 3 3 ... #.# ... 3 3 ### ..# #.#
Sample Output
Case 1: 1 Case 2: -1 Case 3: 0 Case 4: 2 很经典的一道bfs搜索题。大致题意为,两个熊孩子想把草坪给引燃,两个孩子可以从同一块草地点火也可以从不同的两块草地点火,火苗只有碰见草地时候才会燃烧,遇到 空地会阻断燃烧,火苗可以向东西南北中五个方向燃烧。 问如果想把整个草坪的草全部燃烧完,可不可能,不可能的话输出-1,可能的话输出最小的燃烧时间(已知每次燃烧花费1个时间,起始位置不花费时间)。 可以先将所有的草地放在容器中,然后两两的同时进行bfs搜索,再用一个变量记录每次燃烧完所花费的时间,每次搜索都更新下最小值。两两都搜索完的时候,最小的值就是 答案。#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<vector> #include<queue> #include<map> #define INF 1<<30 #define N 15 using namespace std; struct node { int x,y,step; }; int m,n,Count,ans,book[N][N]; char s[N][N]; vector<node>v; void BFS(node a,node b) { int next[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}; queue<node>q; int k,tt=2; if(a.x==b.x&&a.y==b.y) tt--; memset(book,0,sizeof(book)); while(!q.empty()) q.pop(); q.push(a); q.push(b); book[a.x][a.y]=book[b.x][b.y]=1; while(!q.empty()) { a=q.front(); q.pop(); for(k=0; k<4; k++) { b.x=a.x+next[k][0]; b.y=a.y+next[k][1]; b.step=a.step+1; if(b.x<0||b.y<0||b.x>m-1||b.y>n-1) continue; if(book[b.x][b.y]==0&&s[b.x][b.y]=='#') { book[b.x][b.y]=1; q.push(b); tt++;//记录下来一共搜索到了多少块草地; } } } if(tt==Count)//如果刚好搜索完所有的草地,更新搜索的额最小值; ans=min(ans,a.step); } int main() { int t,i,j; scanf("%d",&t); int k=0; while(t--) { v.clear(); Count=0; scanf("%d%d",&m,&n); for(i=0; i<m; i++) for(j=0; j<n; j++) { scanf(" %c",&s[i][j]); if(s[i][j]=='#') { v.push_back((node){i,j,0}); Count++; } } ans=INF; for(i=0; i<v.size(); i++) for(j=i; j<v.size(); j++) BFS(v[i],v[j]); if(ans==INF) printf("Case %d: -1\n",++k); else printf("Case %d: %d\n",++k,ans); } }