FZUOJ 2150 Problem 2150 Fire Game (双起点BFS)

来源:互联网 发布:淘宝女装,连衣裙 编辑:程序博客网 时间:2024/05/01 02:16
 Problem 2150 Fire Game

Accept: 1133    Submit: 4047
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

43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#

 Sample Output

Case 1: 1Case 2: -1Case 3: 0Case 4: 2



题意:给你一个n*m的地图,上面的'.'表示地面,'#'表示草地,要从两个草地开始点火,1s扩散一个格,遇到空地停止,求最少需要的时间
思路:找出草地的位置存入一个数组,然后枚举两个着火点进行BFS,求最小的时间就行了,注意:当草地数<=2的时候,耗费为0,其实是为了避免草地数为0或1时候输出-1的情况。
ac代码:
#include<stdio.h>#include<string.h>#include<queue>#include<iostream>#include<algorithm>#define INF 0xfffffffusing namespace std;char map[11][11];int v[11][11];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};int ans,n,m;struct s{int x,y;int step;};struct ss{int x,y;}list[110];int check(int xx,int yy){if(xx<0||xx>=n||yy<0||yy>=m||v[xx][yy]||map[xx][yy]=='.')return 0;return 1;}void bfs(ss aa,ss bb){s a,b;queue<s>q;memset(v,0,sizeof(v));a.x=aa.x;a.y=aa.y;a.step=0;v[a.x][a.y]=1;b.x=bb.x;b.y=bb.y;b.step=0;v[b.x][b.y]=1;q.push(a);q.push(b);while(!q.empty()){a=q.front();q.pop();ans=max(ans,a.step);for(int i=0;i<4;i++){int nx=a.x+dir[i][0];int ny=a.y+dir[i][1];if(check(nx,ny)){b.x=nx;b.y=ny;b.step=a.step+1;v[b.x][b.y]=1;q.push(b);}}}}int main(){int i,t,j;int cas=0;scanf("%d",&t); while(t--){scanf("%d%d",&n,&m);for(i=0;i<n;i++)scanf("%s",map[i]);int cnt=0;for(i=0;i<n;i++){for(j=0;j<m;j++){if(map[i][j]=='#'){list[cnt].x=i;list[cnt].y=j;cnt++;}}}//for(i=0;i<cnt;i++)//printf("%d %d\n",list[i].x,list[i].y);int Ans=INF;for(i=0;i<cnt;i++){for(j=i+1;j<cnt;j++){ans=-1;bfs(list[i],list[j]);int bz=0;for(int k=0;k<n;k++){for(int p=0;p<m;p++){if(map[k][p]=='#'&&!v[k][p]){bz=1;break;}}if(bz)break;}if(bz==0)Ans=min(Ans,ans);}}printf("Case %d: ",++cas);if(cnt<=2){printf("0\n");continue;}if(Ans==INF)printf("-1\n");elseprintf("%d\n",Ans);}return 0;}



0 0