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
题意:给出一个长着草和空地的地图,让你去求点燃两处之后直到烧光所有的草所需要的的最短时间。
思路:把所有的草所在的坐标存入队列中,从队列中选择两个进行搜索,直到把所有可能的结果搜索完毕。
AC代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int book[15][15]; 6 int a[100][100][2]; 7 int b[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; 8 struct note 9 { 10 int x,y; 11 }; 12 int main() 13 { 14 struct note que[150]; 15 int t,n,m,tmp; 16 char s[15][15]; 17 while(~scanf("%d",&t)) 18 { 19 for(int k=1; k<=t; k++) 20 { 21 scanf("%d%d",&n,&m); 22 tmp=0; 23 for(int i=0; i<n; i++) 24 { 25 scanf("%s",s[i]); 26 for(int j=0; j<m; j++) 27 if(s[i][j]=='#') 28 { 29 que[tmp].x=i; 30 que[tmp].y=j; 31 tmp++; 32 } 33 } 34 printf("Case %d: ",k); 35 if(tmp<=2) 36 { 37 printf("0 "); 38 continue; 39 } 40 int num,tp,tq,tl,ans; 41 num=200; 42 for(int i=0; i<tmp; i++) 43 { 44 for(int j=i+1; j<tmp; j++) 45 { 46 memset(book,0,sizeof(book)); 47 a[0][0][0]=que[i].x; 48 a[0][0][1]=que[i].y; 49 a[0][1][0]=que[j].x; 50 a[0][1][1]=que[j].y; 51 book[a[0][0][0]][a[0][0][1]]=1; 52 book[a[0][1][0]][a[0][1][1]]=1; 53 tp=1; 54 tl=2; 55 ans=2; 56 for(int p=0;; p++) 57 { 58 tq=tl; 59 tl=0; 60 for(int q=0; q<tq; q++) 61 { 62 for(int l=0; l<4; l++) 63 { 64 if(s[a[p][q][0]+b[l][0]][a[p][q][1]+b[l][1]]=='.'||a[p][q][0]+b[l][0]<0||a[p][q][0]+b[l][0]>=n||a[p][q][1]+b[l][1]<0||a[p][q][1]+b[l][1]>=m) 65 continue; 66 if(s[a[p][q][0]+b[l][0]][a[p][q][1]+b[l][1]]=='#'&&book[a[p][q][0]+b[l][0]][a[p][q][1]+b[l][1]]==0) 67 { 68 a[p+1][tl][0]=a[p][q][0]+b[l][0]; 69 a[p+1][tl][1]=a[p][q][1]+b[l][1]; 70 book[a[p][q][0]+b[l][0]][a[p][q][1]+b[l][1]]=1; 71 tl++; 72 } 73 } 74 tp=p; 75 } 76 ans=ans+tl; 77 if(tl==0) 78 break; 79 } 80 if(ans==tmp) 81 num=min(num,tp); 82 } 83 } 84 if(num==200) 85 num=-1; 86 printf("%d ",num); 87 } 88 } 89 return 0; 90 }
心得体会:其实没有想到这样的代码能够AC的,担心会超时。就想着先写着交交看,TLE的话再进行优化,收获是要敢写。