Title Link: http://acm.fzu.edu.cn/problem.php?pid=2150
Problem Description
Fat brother and Maze is playing a kind of special (hentai) game on an n*m board (N rows, M columns). At the beginning, each of the grids of this board are consisting of grass or just empty and then they start-to-fire all the grass. Firstly they choose, grids which is 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 are adjacent to this grid would 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 the no new grid get fire. If then all the grid which be consisting of grass is get fired, Fat brother and Maze would 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 this grass in the board would never burn out and the empty grid would never get fire.
Note that the both grids they choose can be the the same.
Input
The first line of the date is a integer T, which is the number of the text cases.
Then T cases follow, each case contains the 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 this there is at least one grid which are consisting of grass in the board.
1 <= T <=100, 1 <= n <=10, 1 <= m <=10
Output
For each case, output of 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 Input4 3 3. #. ### .#. 3 3. #. #.# .#. 3 3 ... #.# 3 3 # # # #. # #.# Sample outputcase 1:1case 2: -1case 3:0case 4:2The main idea: Two people play about the fire game, on a map # means hay, start can ignite two hay (can overlap), fire can spread to the surrounding four direction, the gap can not be too, ask all the hay can burn, if can, output the fastest timeidea: The data in the question is not big, all the hay coordinate statistics, each time to elect two to iterate over, statistics all possible minimum values
#include <stdio.h>#include<cstring>#include<cstdlib>#include<algorithm>#include<math.h>#include<queue>using namespacestd;#defineINF 0x3f3f3f3f#definell Long Long#defineMet (b) memset (A,b,sizeof (a))#defineN 109intVis[n][n];CharStr[n][n];intdir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};intk,n,m;structnode{intX,y,temp;} A[n];///statistics on the coordinates of HayintPan ()///determine if the hay can be ignited{ intx, y; for(intI=0; i<k;i++) {x=a[i].x;y=a[i].y; if(!Vis[x][y])return 0; } return 1;}intBFS (node A,node b) {intteam=0; Met (Vis,0); Queue<node>p; Node q,p; Q.push (a); Q.push (b); VIS[A.X][A.Y]=1; vis[b.x][b.y]=1; while(Q.size ()) {Q=Q.front (); Q.pop (); for(intI=0;i<4; i++) {p.x=q.x+dir[i][0]; P.Y=q.y+dir[i][1]; P.temp=q.temp+1; if(p.x>=0&& p.x<n&& p.y>=0&& p.y<m && str[p.x][p.y]=='#'&&!VIS[P.X][P.Y]) {VIS[P.X][P.Y]=1; Team=Max (team,p.temp); Q.push (P); } } } if(Pan ())returnteam; Else returnINF;}intMain () {intt,con=1; scanf ("%d",&t); while(t--) {scanf ("%d%d",&n,&m); for(intI=0; i<n;i++) scanf ("%s", Str[i]); K=0; for(intI=0; i<n;i++) { for(intj=0; j<m;j++) { if(str[i][j]=='#') {a[k].x=i; A[k].y=J; A[k++].temp=0; } } } intans=INF; for(intI=0; i<k;i++) { for(intj=i;j<k;j++) {ans=min (BFS (a[i],a[j]), ans); }} printf ("Case %d:", con++); if(ans==INF) printf ("-1\n"); Elseprintf ("%d\n", ans); } return 0;}
(Fzu 2150) Fire Game (BFS)