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:

  • #, 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.

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Output Specification

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.

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; }
复制代码

 

posted @   Asimple  阅读(222)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2016-05-22 ACM题目————The Blocks Problem
2016-05-22 ACM学习之路————一个大整数与一个小整数不得不说得的秘密
2016-05-22 ACM题目————Anagram
2016-05-22 ACM题目————马拦过河卒
点击右上角即可分享
微信分享提示