【双向搜索】Fire Game
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.
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
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.
4 3 3 .#. ### .#. 3 3 .#. #.# .#. 3 3 ... #.# ... 3 3 ### ..# #.#
Case 1: 1 Case 2: -1 Case 3: 0 Case 4: 2解法:双向搜索的基础题。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 #include <stack> 4 #define INF 99999999 5 #define MAX 15 6 using namespace std; 7 char Map[MAX][MAX]; 8 int Num[4][2]={1,0,-1,0,0,1,0,-1}; 9 int D[100861],X[100861],Y[100861]; 10 int BFS(int x0,int y0,int x1,int y1,int Sum) 11 { 12 int i,j,t=0,x,y,xx,yy,Max=0; 13 int MAP[MAX][MAX]={0}; 14 int S=0,E=2; 15 MAP[x0][y0]=1; 16 MAP[x1][y1]=1; 17 D[0]=0;D[1]=0; 18 X[0]=x0;Y[0]=y0; 19 X[1]=x1;Y[1]=y1; 20 while(S<E) 21 { 22 xx=X[S];yy=Y[S++]; 23 for(i=0;i<4;i++) 24 { 25 x=Num[i][0]+xx;y=Num[i][1]+yy; 26 if(!MAP[x][y]&&Map[x][y]=='#') 27 { 28 D[E]=D[S-1]+1; 29 if(Max<D[E])Max=D[E]; 30 X[E]=x;Y[E++]=y; 31 MAP[x][y]=1; 32 t++; 33 } 34 } 35 } 36 if(t==Sum)return Max; 37 else return INF; 38 } 39 int main() 40 { 41 int N,K,i,j,k,l,M,Min,Sign,T,Sum,tt=1; 42 scanf("%d",&T); 43 while(T--) 44 { 45 scanf(" %d %d",&N,&M);Sum=0; 46 for(i=0;i<=N+1;i++) 47 { 48 for(j=0;j<=M+1;j++) 49 { 50 if(i==0||j==0||i==N+1||j==M+1)Map[i][j]='.'; 51 else scanf(" %c",&Map[i][j]); 52 if(Map[i][j]=='#')Sum++; 53 } 54 } 55 for(i=1,Min=INF;i<=N;i++) 56 { 57 for(j=1;j<=M;j++) 58 { 59 for(k=1;k<=N;k++) 60 { 61 for(l=1;l<=M;l++) 62 { 63 if(Map[i][j]=='.'||Map[k][l]=='.')continue; 64 if(i==k&&j==l) Sign=BFS(i,j,k,l,Sum-1); 65 else Sign=BFS(i,j,k,l,Sum-2); 66 if(Sign<Min)Min=Sign; 67 } 68 } 69 } 70 } 71 printf("Case %d: ",tt++); 72 if(Min==INF)printf("-1\n"); 73 else printf("%d\n",Min); 74 } 75 return 0; 76 }
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************