UVA11624 Fire!
来源:互联网 发布:手机淘宝怎么关注 编辑:程序博客网 时间:2024/06/05 23:35
Fire!
Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of themaze are on fire, you must determine whether Joe can exit the maze before thefire 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 fourdirections from each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the fire may enter asquare that is occupied by a wall.
Input Specification
The first line of input contains a single integer, thenumber of test cases to follow. The first line of each test case contains thetwo integers R and C, separated byspaces, with 1 <= R,C <= 1000. Thefollowing R lines of the test case each contain one row of themaze. Each of these lines contains exactly C characters, andeach of these characters is one of:
· #, a wall
· ., a passable square
· J, Joe's initialposition in the maze, which is a passable square
· F, a square that is onfire
There will be exactly one J in each test case.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Output Specification
For each test case, output a single line containing IMPOSSIBLE if Joe cannotexit the maze before the fire reaches him, or an integer giving the earliesttime Joe can safely exit the maze, in minutes.
Output for SampleInput
3
IMPOSSIBLE
题意:J希望能走出这个迷宫,然后F表示火苗的位置,然后F每秒可以向四个方向扩展。
思路:把F扩展的格子标记上最少的秒数,利用bfs最短路的应用,标记出来。
然后bfs一次J看是否能走到边界处即可。别忘了加火苗限制条件,秒数要比标记的秒数小才可以。
#include <cstdio>#include <queue>#include <cstring>using namespace std;const int MAXN=1000+7;const int inf=1e9;int n ,m;int sx,sy;char tu[MAXN][MAXN];bool vis[MAXN][MAXN];int step[MAXN][MAXN];int dx[4]={0,0,1,-1};int dy[4]={1,-1,0,0};struct node{ int x,y,step; node(int a,int b,int c) { x=a; y=b; step=c; }};void bfs(int sx,int sy){ queue<node>q; memset(vis,0,sizeof(vis)); vis[sx][sy]=1; q.push(node(sx,sy,0)); while(!q.empty()) { node u=q.front(); q.pop(); if(u.x == 0 || u.x == n-1 || u.y == 0 || u.y == m-1) { printf("%d\n",u.step+1); return ; } for(int i = 0;i < 4 ; ++i) { int nx=u.x+dx[i]; int ny=u.y+dy[i]; if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && tu[nx][ny]!='#' && u.step+1 < step[nx][ny]) { vis[nx][ny]=1; q.push(node(nx,ny,u.step+1)); } } } puts("IMPOSSIBLE");}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); queue<node>q; for(int i = 0 ; i < n ; ++i ) for(int j = 0 ;j < m ; ++j) { vis[i][j]=0; step[i][j]=inf; } for(int i = 0 ; i < n ; ++i) { scanf("%s",tu[i]); for(int j = 0 ; j < m ; ++j) { if(tu[i][j]=='F') { q.push(node(i,j,0)); vis[i][j]=1; } else if(tu[i][j]=='J') { sx=i; sy=j; } } } //bfs出火苗覆盖到每个空点的时间 while(!q.empty()) { node u=q.front(); q.pop(); step[u.x][u.y]=u.step; for(int i = 0;i < 4;++i) { int nx=u.x+dx[i]; int ny=u.y+dy[i]; if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && tu[nx][ny] != '#') { vis[nx][ny] = 1; q.push(node(nx ,ny ,u.step+1)); } } } // bfs(sx,sy); } 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!
- ReactiveCocoa入门(2)元祖、json解析、应用
- 自定义View——弹性滑动
- [leetcode: Python]438. Find All Anagrams in a String
- xbox360 ubuntu14.04连接问题
- SVN服务器搭建和使用(二)
- UVA11624 Fire!
- 一个简单易用的容器管理平台-Humpback
- simple and stupid
- 用递归方法求 n 阶勒让德多项式的值,递归公式为
- KMP算法
- 水题引发的对数学的感想
- Tips for 前端
- vue、rollup、sass、requirejs组成的vueManager
- 深度解析 Java 计算某个文件的 MD5 值的原理(附源码)