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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2016-05-22 ACM题目————The Blocks Problem
2016-05-22 ACM学习之路————一个大整数与一个小整数不得不说得的秘密
2016-05-22 ACM题目————Anagram
2016-05-22 ACM题目————马拦过河卒