uva11624 Fire! (双bfs)
来源:互联网 发布:四川网络电视剧节目表 编辑:程序博客网 时间:2024/06/01 09:27
Joe works in a maze. Unfortunately, portions of the maze have
caught on fire, and the owner of the maze neglected to create a fire
escape plan. Help Joe escape the maze.
Given Joe’s location in the maze and which squares of the maze
are on fire, you must determine whether Joe can exit the maze before
the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or
horizontally (not diagonally). The fire spreads all four directions
from each square that is on fire. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the fire
may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test
cases to follow. The first line of each test case contains the two
integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The
following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
C characters, and each of these characters is one of:
• #, a wall
• ., a passable square
• J, Joe’s initial position in the maze, which is a passable square
• F, a square that is on fire
There will be exactly one J in each test case.
Output
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the
fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output
3
IMPOSSIBLE
题目链接
思路:由于题中没有说有几个起火点 所以 要用一个队列来维护。
当在T秒时人移动过后 对火点进行扩散
#include <stdio.h>#include <queue>#include <string.h>using namespace std;char map[1005][1005];bool vis[1005][1005];bool vis1[1005][1005];int dir[4][2]={1,0,-1,0,0,1,0,-1};int n,m,st_x,st_y;struct node{int x,y,t;friend bool operator <(node a,node b){return a.t>b.t;}};struct node1{int x,y,t;};bool limit(int x,int y){if(x<0||x==n||y<0||y==m||vis[x][y]||map[x][y]=='#'||map[x][y]=='F')return false;return true;}queue<node1>fire;void fire_spread(){node1 temp1,temp2;int T=fire.front().t;while(!fire.empty()){temp1=temp2=fire.front();//printf("**%d %d %d**\n",temp1.x,temp1.y,temp1.t);if(temp1.t>T)break;fire.pop();for(int i=0;i<4;i++){int xx=temp1.x+dir[i][1];int yy=temp1.y+dir[i][0];if(!vis1[xx][yy]&&map[xx][yy]!='#'&&xx>=0&&xx<n&&yy>=0&&yy<m){//printf("%d %d %d\n",xx,yy,temp1.t);vis1[xx][yy]=true;vis[xx][yy]=true;temp1.x=xx;temp1.y=yy;temp1.t++;fire.push(temp1);}temp1=temp2;}}}int bfs(){node temp1,temp2;priority_queue<node>s;while(!s.empty()) s.pop(); temp1.x=st_x,temp1.y=st_y,temp1.t=0;s.push(temp1);while(!s.empty()) {temp1=temp2=s.top();s.pop();//要让所有t时间的人走完 才对火进行扩散 if(temp1.t>fire.front().t)fire_spread();if(vis[temp1.x][temp1.y]) continue;if(temp1.x==0||temp1.x==n-1||temp1.y==0||temp1.y==m-1)return temp1.t;vis[temp1.x][temp1.y]=true;for(int i=0;i<4;i++){int xx=temp1.x+dir[i][0];int yy=temp1.y+dir[i][1];if(limit(xx,yy)){temp1.x=xx;temp1.y=yy;temp1.t++;s.push(temp1);}temp1=temp2;}}return -1;}int main(){int ncase;scanf("%d",&ncase);while(ncase--){memset(map,false,sizeof(map));memset(vis,false,sizeof(vis));memset(vis1,false,sizeof(vis1));while(!fire.empty()) fire.pop();scanf("%d %d",&n,&m);for(int i=0;i<n;i++){getchar();for(int j=0;j<m;j++){scanf("%c",&map[i][j]);if(map[i][j]=='F'){node1 temp;vis[i][j]=vis1[i][j]=true;temp.x=i,temp.y=j,temp.t=0;fire.push(temp);}if(map[i][j]=='J')st_x=i,st_y=j;}}int result=bfs();if(result==-1)printf("IMPOSSIBLE\n");elseprintf("%d\n",result+1);}return 0;}
- uva11624 Fire! (双bfs)
- UVA11624-Fire!(两次bfs)
- [bfs] UVA11624-Fire!
- UVA11624 Fire BFS
- UVA11624 Fire! BFS
- UVA11624 Fire 两次BFS
- UVA11624 Fire!【BFS+细心】
- UVa11624-Fire!(bfs)
- Uva11624 Fire(bfs 搜索)
- uva11624 fire bfs 最短路
- UVa11624 Fire!(BFS+迷宫)
- UVA11624:Fire!(BFS + 优化)
- UVA11624 Fire! —— BFS
- UVA11624 Fire! 两次BFS 读懂题意很重要
- UVA11624 Fire! (两点广搜BFS)
- UVA11624 Fire! 两次BFS(J)
- Fire! UVA11624
- UVA11624 Fire!
- linux centos 和ubuntu 的安装命令
- NOIP2011铺地毯
- Device is offline
- 猴子吃桃-倒推法
- 微信支付遇到的超级蛋疼的事
- uva11624 Fire! (双bfs)
- 基因数据处理39之mango安装记录
- 无约束极值、线性分类器和线性回归基础
- mmap的使用之两个进程通过映射普通文件实现共享内存通信
- JavaScript学习--Item16 for 循环和for...in 循环的那点事儿
- 安卓初识基本控件_Gallery
- iOS 数据库篇8—FMDB数据库队列
- hdu 4183(最大流)
- 使用VS进行打包