FZU 2150 Fire Game (BFS)
Problem 2150 Fire Game
Accept: 412 Submit: 1638
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem 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
题目意思就是开始选任意两点有草的地方放火烧草,每一秒被点燃的地方都会向上下左右四个方向蔓延。如果能烧完草,则输出烧完草的时间,否则输出-1
这道题其实思路很明了就是对两个最开始点燃的点进行BFS
但是这道题有很多细节要注意
如果刚开始的草堆的数目小于2的情况,那说明第一次就能把草全部烧完,输出-1是草不能烧完才输出-1
剪枝
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<queue> 5 #include<cstring> 6 #include<stdlib.h> 7 #include<algorithm> 8 using namespace std; 9 char a[15][15]; 10 int vis[15][15]; 11 int wis[15][15][15][15]; 12 int ans,sum,starx,stary,starxx,staryy,n,m,mark,all; 13 int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; 14 const int INF=0x3f3f3f3f; 15 16 struct node 17 { 18 int x,y; 19 int step; 20 }; 21 node p,q; 22 23 int panduan(int i,int j) 24 { 25 if(0<=i&&i<n && 0<=j&&j<m && a[i][j]=='#') 26 return 1; 27 return 0; 28 } 29 30 void BFS() 31 { 32 memset(vis,0,sizeof(vis)); 33 queue <node> Q; 34 sum=-INF;//sum用来记录每一次用的最大时间 35 vis[starx][stary]=1; 36 vis[starxx][staryy]=1; 37 38 p.x=starx; 39 p.y=stary; 40 p.step=0; 41 42 q.x=starxx; 43 q.y=staryy; 44 q.step=0; 45 46 Q.push(p); 47 Q.push(q); 48 49 while(!Q.empty()) 50 { 51 node now,next; 52 now=Q.front(); 53 Q.pop(); 54 55 for(int i=0;i<4;i++) 56 { 57 next.x=now.x+dir[i][0]; 58 next.y=now.y+dir[i][1]; 59 if(panduan(next.x,next.y) && !vis[next.x][next.y]) 60 { 61 vis[next.x][next.y]=1; 62 next.step=now.step+1;//这里不要写成next.step++了 63 all++; 64 Q.push(next); 65 // if(next.step>sum) 66 // sum=next.step; 67 // printf("next.step=%d sum=%d\n",next.step,sum); 68 } 69 } 70 sum=max(sum,now.step); 71 } 72 } 73 int main() 74 { 75 //freopen("in.txt","r",stdin); 76 int kase,cnt=0; 77 scanf("%d",&kase); 78 while(kase--) 79 { 80 memset(wis,0,sizeof(wis)); 81 ans=INF,mark=0; 82 scanf("%d %d",&n,&m); 83 for(int i=0;i<n;i++) 84 { 85 scanf("%s",a[i]); 86 for(int j=0;j<m;j++) 87 if(a[i][j]=='#') 88 mark++; 89 getchar(); 90 } 91 92 if(mark<=2) 93 { 94 printf("Case %d: 0\n",++cnt); 95 continue; 96 } 97 //printf("mark=%d\n",mark); 98 for(int i=0;i<n;i++) 99 { 100 for(int j=0;j<m;j++) 101 { 102 if(a[i][j]!='#') continue;//如果有一个点不是草就不去搜 103 for(int ii=0;ii<n;ii++) 104 { 105 for(int jj=0;jj<m;jj++) 106 { 107 if(panduan(i,j) && panduan(ii,jj) && (i!=ii || j!=jj) )//要搜的两个点,如果两个点重合就不搜 108 { 109 if(wis[i][j][ii][jj]==1) continue;//剪枝不搜之前搜过的情况 110 // printf("%d %d %d %d\n",i,j,ii,jj); 111 wis[i][j][ii][jj]=wis[ii][jj][i][j]=1; 112 starx=i; 113 stary=j; 114 starxx=ii; 115 staryy=jj; 116 all=2; 117 BFS(); 118 // printf("all=%d\n",all); 119 if(ans>sum && mark==all)//用all记录草是否全部烧完了 120 { 121 //printf("%d\n",sum); 122 ans=sum; 123 } 124 } 125 } 126 } 127 } 128 } 129 // printf("ans=%d\nsum=%d\n",ans,sum); 130 if(ans==INF) 131 printf("Case %d: -1\n",++cnt); 132 else 133 printf("Case %d: %d\n",++cnt,ans); 134 } 135 return 0; 136 }