FZUOJ 2150 Problem 2150 Fire Game (双起点BFS)
来源:互联网 发布:淘宝女装,连衣裙 编辑:程序博客网 时间:2024/05/01 02:16
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
Sample Output
#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;}
- FZUOJ 2150 Problem 2150 Fire Game (双起点BFS)
- FZU Problem 2150 Fire Game (双起点BFS啊 )
- fzuoj 2150 Fire Game BFS
- FZU 2150 Fire Game (双起点BFS)
- FZU 2150 Fire Game 多起点BFS
- FZU 2150 Fire Game 多起点BFS
- FZU 2150 Fire Game(多起点bfs)
- Fire Game (双起点bfs)
- I - Fire Game FZU 2150 双起点的BFS
- FZU Problem 2150 Fire Game (BFS,java)
- Fire Game (多起点BFS)
- Problem 2150 Fire Game
- Problem 2150 Fire Game
- FZU - 2150 Fire Game(bfs)
- 【Fzu】2150 Fire Game(BFS)
- FZU - 2150 Fire Game(两点bfs)
- FZU 2150 Fire Game (暴力BFS)
- FZU 2150 Fire Game (DFS + BFS)
- MATLAB向txt连续写入数据
- 使用服务开启关闭暂停音乐
- Python一周学习的小结
- 犀牛书随手记-05
- 第2章第3节练习题1 串的模式匹配(Naive)
- FZUOJ 2150 Problem 2150 Fire Game (双起点BFS)
- Android-Uiautomator:[6]运行中传入参数
- Scala-01:函数定义、流程控制、异常处理
- Android笔记--对同步与异步的概念上的理解和对LayoutParams的理解
- 一起学CC3200之开发环境简介(2)烧录程序
- C#窗体自适应
- 应用程序之间的跳转设置info.plist
- 数列排序
- 算法导论 Exercises 9.3-9