UVA - 11624 Fire!

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 Specification

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:

  • <tt>#</tt>, a wall
  • <tt>.</tt>, a passable square
  • <tt>J</tt>, Joe's initial position in the maze, which is a passable square
  • <tt>F</tt>, a square that is on fire

There will be exactly one <tt>J</tt> 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 <tt>IMPOSSIBLE</tt> 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.

Output for Sample Input

3
IMPOSSIBLE

题目大意:

有个人 J 被困于火场中。火焰 F 每分钟蔓延1格,人每分钟移动一格,求出人是否会被困于火场,或者逃脱,如果逃脱,求出最短的时间。

坑点: F不止一个(被样例误导了) 

//Asimple
#include <bits/stdc++.h> //#define INF 0x3fffffff #define mod 10007 #define swap(a,b,t) t = a, a = b, b = t #define CLS(a, v) memset(a, v, sizeof(a)) #define debug(a) cout << #a << " = " << a <<endl using namespace std; typedef long long ll; const int maxn = 1000+5; const double PI=acos(-1.0); const ll INF = 0x3f3f3f ; const int dx[] = {-1, 0, 1, 0}; const int dy[] = { 0,-1, 0, 1}; int n, m, res, ans, len, T, k, num, sum, x, y; char Map[maxn][maxn]; bool vis[maxn][maxn]; int dis[maxn][maxn]; struct node{ int x, y, v; node(){ } node(int x, int y, int v):x(x),y(y),v(v){ } }; void bfs_f(){ queue<node> q; CLS(vis, false); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if( Map[i][j]=='F' ) { vis[i][j] = true; dis[i][j] = 0; q.push(node(i, j, 0)); } } } while( !q.empty() ) { node now = q.front(); q.pop(); for(int i=0; i<4; i++) { int x = now.x + dx[i]; int y = now.y + dy[i]; int v = now.v+1; if( x<0 || x>=n || y<0 || y>=m || Map[x][y]=='#' || Map[x][y]=='F' || vis[x][y]) continue; dis[x][y] = v; vis[x][y] = true; q.push(node(x, y, dis[x][y])); } } } int solve(int x, int y){ queue<node> q; CLS(vis, false); q.push(node(x, y, 0)); vis[x][y] = true; while( !q.empty() ) { node now = q.front(); q.pop(); for(int i=0; i<4; i++) { x = now.x + dx[i]; y = now.y + dy[i]; int v = now.v + 1; if( x<0 || y<0 || x>=n || y>=m ) return v; if( dis[x][y]<=v || Map[x][y]=='#' || Map[x][y]=='F' || vis[x][y] ) continue; vis[x][y] = true; q.push(node(x, y, v)); } } return 0; } void input() { ios_base::sync_with_stdio(false); scanf("%d", &T); while( T -- ) { scanf("%d%d",&n,&m); for(int i=0; i<n; i++){
    scanf("%s", Map[i]);    for(int j=0; j<m; j++) { dis[i][j] = INF; if( Map[i][j]=='J' ) { x = i; y = j; } } } bfs_f(); ans = solve(x, y); if( ans ) printf("%d\n", ans); else printf("IMPOSSIBLE\n"); } } int main(){ input(); return 0; }

 

全部评论

相关推荐

门口唉提是地铁杀:之前b站被一个游戏demo深深的吸引了。看up主页发现是个初创公司,而且还在招人,也是一天60。二面的时候要我做一个登录验证和传输文件两个微服务,做完要我推到github仓库,还要我加上jaeger和一堆运维工具做性能测试并且面试的时候投屏演示。我傻乎乎的做完以后人家跟我说一句现在暂时不招人,1分钱没拿到全是白干
点赞 评论 收藏
分享
明天不下雨了_人机版:让我们大声的说出来:以前的未来就是现在
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务