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
Sample Output
题目大意:
就是有两个人,他们想在n*m的草坪里OOXX,所以他们要把草坪里的草都烧光,就是问怎么样才能在最短时间
内烧完这些草(两个人可以同时烧一块草)
解题思路:
首先可以想到这是一个BFS, 然后分情况讨论,
1)当# <= 2的时候i,就肯定是0
2)当#的块数>2的时候,就肯定输出-1;
3)剩下的情况就是bfs了,只是还得考虑一下:是最长的路和最短的时间,
(因为可能有两块不一样的草坪,取那个长的的时间作为结果)
上代码:
/** 2015 - 09 - 17 晚上 Author: ITAK Motto: 今日的我要超越昨日的我,明日的我要胜过今日的我, 以创作出更好的代码为目标,不断地超越自己。 **/ #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <algorithm> #include <set> using namespace std; typedef long long LL; const int maxn = 15; const double eps = 1e-7; bool vis[maxn*10][maxn*10]; const int dir[4][2]= {1, 0, 0, 1, -1, 0, 0, -1}; char map[maxn][maxn]; int m, n; struct node { int x, y, step; }arr[maxn*10]; ///p 和 q 表示那两个人放火的坐标 struct node p, q; ///以下就是bfs的基本套路,注意返回的最长的路径 int bfs(int x1, int y1, int x2, int y2) { int Max = -99999; queue<node>que; p.x = x1, p.y = y1, p.step = 0; q.x = x2, q.y = y2, q.step = 0; que.push(p), que.push(q); while(!que.empty()) { node end, start; start = que.front(); que.pop(); for(int i=0; i<4; i++) { int dx = start.x + dir[i][0]; int dy = start.y + dir[i][1]; if(!vis[dx][dy] && map[dx][dy]=='#' && dx>=0&&dx<m && dy>=0&&dy<n) { vis[dx][dy] = true; end.x = dx; end.y = dy; end.step = start.step + 1; que.push(end); } } Max = max(Max, start.step); } return Max; } int main() { int T; scanf("%d",&T); for(int cas=1; cas<=T; cas++) { scanf("%d%d",&m, &n); int cnt = 0;///有多少个'#' for(int i=0; i<m; i++) { scanf("%s",map[i]); for(int j=0; j<n; j++) { if(map[i][j] == '#') { cnt++; arr[cnt].x = i; arr[cnt].y = j; } } } printf("Case %d: ",cas); if(cnt <= 2)///如果小于2的话,肯定就是0了 { puts("0"); continue; } int ans = 999999;///最终的答案 for(int i=0; i<cnt; i++) { for(int j=i; j<cnt; j++) { memset(vis, false, sizeof(vis)); vis[arr[i].x][arr[i].y] = true; vis[arr[j].x][arr[j].y] = true; bool ok = false; int Min = bfs(arr[i].x, arr[i].y, arr[j].x, arr[j].y); ///枚举最长的块中的每个点 for(int ii=0; ii<m; ii++) { for(int jj=0; jj<n; jj++) { if(map[ii][jj] != '#') continue; if(!vis[ii][jj]) { ok = true; continue; } } if(ok) break; } if(!ok) ans = min(Min, ans); } } if(ans == 999999) puts("-1"); else cout<<ans<<endl; } return 0; } /** 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 **/