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; }

 

全部评论

相关推荐

程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
03-26 13:44
南华大学 Java
在看面经的花生米很野蛮:这种情况下你当然要回答,你也是吗!!!!我超喜欢他的XXXXX
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务