UVA11624 Fire!
来源:互联网 发布:深圳中设软件 编辑:程序博客网 时间:2024/05/29 10:47
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.
First calculate the time that each blocks got fire,and use bfs to solve.I learned that usage of continue will quicken the process.And yes,it’s two bfs.
#include<iostream>#include<queue>#include<vector>#include<cstring>#include<cstdio>#include<cmath>const int MAX=1010;int fire[MAX+5][MAX+5];char m[MAX+5][MAX+5];int vis[MAX+5][MAX+5];int dx[4]={0,0,1,-1};int dy[4]={1,-1,0,0};int N,M;using namespace std;queue<pair<int,int> > q;bool check(int x,int y){ if(x>=0&&y>=0&&x<N&&y<M&&m[x][y]!='#') { return true; } return false;}void bfs1(){ while(!q.empty())q.pop(); memset(fire,-1,sizeof(fire)); for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { if(m[i][j]=='F') { fire[i][j]=0; q.push(make_pair(i,j)); } } } while(!q.empty()) { pair<int,int> temp=q.front(); int x=temp.first; int y=temp.second; q.pop(); for(int i=0;i<4;i++) { int xx=x+dx[i]; int yy=y+dy[i]; if(xx<0||yy<0||xx>=N||yy>=M)continue; if(fire[xx][yy]!=-1)continue; if(m[xx][yy]=='#')continue; fire[xx][yy]=fire[x][y]+1; q.push(make_pair(xx,yy)); } }}int bfs(int x,int y){ memset(vis,-1,sizeof(vis)); while(!q.empty())q.pop(); vis[x][y]=0; q.push(make_pair(x,y)); while(!q.empty()) { pair<int,int> temp=q.front(); q.pop(); x=temp.first; y=temp.second; if(x==0||y==0||x==N-1||y==M-1) { return vis[x][y]+1; } for(int i=0;i<4;i++) { int xx=x+dx[i]; int yy=y+dy[i]; if(xx<0||yy<0||xx>=N||yy>=M)continue; if(vis[xx][yy]!=-1)continue; if(m[xx][yy]=='#')continue; if(fire[xx][yy]!=-1 && vis[x][y]+1>=fire[xx][yy])continue; vis[xx][yy]=vis[x][y]+1; q.push(make_pair(xx,yy)); } } return -1;}int main(){ int sx,sy; int t; scanf("%d",&t); while(t--) { scanf("%d%d",&N,&M); for(int i=0;i<N;i++) { scanf("%s",m[i]); } for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { if(m[i][j]=='J') { sx=i; sy=j; } } } bfs1(); int ans=bfs(sx,sy); if(ans!=-1) { printf("%d\n",ans); } else { printf("IMPOSSIBLE\n"); } } return 0;}
- Fire! UVA11624
- UVA11624 Fire!
- uva11624 - Fire!
- uva11624 Fire!
- UVA11624 Fire!
- UVa11624 Fire!
- UVA11624 Fire!
- UVA11624----Fire!
- UVA11624 Fire!
- UVA11624 Fire!
- UVA11624 Fire!
- UVA11624 Fire!
- UVA11624 Fire!
- UVA11624 Fire!
- 【UVA11624】Fire!
- UVA11624-Fire!
- UVA11624-Fire!(两次bfs)
- [bfs] UVA11624-Fire!
- ffmpeg综合应用示例(三)——安卓手机摄像头编码
- 第14章 使用awk处理文本
- Android的动画
- Lucene初探之数据格式详情(二)
- 关于Android性能优化的若干方案
- UVA11624 Fire!
- 修改host,上github
- ubuntu上安装hbase
- C~K祝你元宵节快乐!—结构体数组+3重for循环暴力求解
- Android应用开发SharedPreferences存储数据的使用方法
- rem布局,系统字体大小发生变化是,页面样式错乱的问题
- Linux用户获得超级管理员权限
- [LeetCode] Next Greater Element I
- phpjson多层乱码解决