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

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-21 17:16
科大讯飞 算法工程师 28.0k*14.0, 百分之三十是绩效,惯例只发0.9
点赞 评论 收藏
分享
10-07 20:48
门头沟学院 Java
听说改名就会有offer:可能是实习上着班想到后面还要回学校给导师做牛马,看着身边都是21-25的年纪,突然emo了了
点赞 评论 收藏
分享
10-13 17:47
门头沟学院 Java
wulala.god:图一那个善我面过,老板网上找的题库面的
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务