UVA11624(KB1-J)
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
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.
Output
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.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output
3
IMPOSSIBLE
//2017-03-01
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; struct node{
int x, y, step;
void setNode(int x, int y, int step){
this->x = x;
this->y = y;
this->step = step;
}
};
char maze[][];
int dis[][], n, m;
bool vis[][];
const int inf = 0x3f3f3f3f;
int dx[] = {, , , -};
int dy[] = {, , -, }; void bfs(int sx, int sy)
{
queue<node> q;
node tmp;
memset(vis, , sizeof(vis));
if(sx == -){
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
if(maze[i][j] == 'F')
{
tmp.setNode(i, j, );
q.push(tmp);
vis[i][j] = ;
dis[i][j] = ;
}
}else{
tmp.setNode(sx, sy, );
q.push(tmp);
vis[sx][sy] = ;
}
int x, y, step, ans;
bool fg = false;
while(!q.empty())
{
x = q.front().x;
y = q.front().y;
step = q.front().step;
q.pop();
if(maze[sx][sy]=='J'&&(x == n- || y == m- || x == || y == )){
fg = true;
ans = step+;
}
for(int i = ; i < ; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&!vis[nx][ny]&&maze[nx][ny]!='#'){
vis[nx][ny] = ;
if(maze[sx][sy]=='J'){
if(step+ >= dis[nx][ny])continue;
else if(nx == n- || ny == m- || nx == || ny == ){
fg = true;
ans = step+;
}
}else dis[nx][ny] = min(dis[nx][ny], step+);
tmp.setNode(nx, ny, step+);
q.push(tmp);
}
}
if(fg)break;
}
if(maze[sx][sy] == 'J')
if(fg)printf("%d\n", ans);
else printf("IMPOSSIBLE\n");
} int main()
{
int T, jx, jy;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
getchar();
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
scanf("%c", &maze[i][j]);
if(maze[i][j] == 'J'){
jx = i; jy = j;
}
}
getchar();
}
memset(dis, inf, sizeof(dis));
bfs(-, -);
bfs(jx, jy);
} return ;
}
UVA11624(KB1-J)的更多相关文章
- uva11624 - Fire!
uva11624 - Fire! 火在蔓延,人在走.火会蔓延,不会熄灭,我们可以确定某个点着火的时间(广搜).对于J来说,要是他走到某点的时间比火蔓延到该点的时间要短,那么他走到该点的时候,火还没蔓延 ...
- UVA11624 Fire! —— BFS
题目链接:https://vjudge.net/problem/UVA-11624 题解: 坑点:“portions of the maze havecaught on fire”, 表明了起火点不唯 ...
- uva11624 Fire! (bfs预处理)
题目链接:https://vjudge.net/problem/UVA-11624 题意:给一个1000×1000的矩阵,有几个着火点和Joe,着火点和Joe每个单位时间均移动一个单位,求Joe逃出的 ...
- 【Java并发编程实战】-----“J.U.C”:CAS操作
CAS,即Compare and Swap,中文翻译为"比较并交换". 对于JUC包中,CAS理论是实现整个java并发包的基石.从整体来看,concurrent包的实现示意图如下 ...
- 【Java并发编程实战】-----“J.U.C”:Exchanger
前面介绍了三个同步辅助类:CyclicBarrier.Barrier.Phaser,这篇博客介绍最后一个:Exchanger.JDK API是这样介绍的:可以在对中对元素进行配对和交换的线程的同步点. ...
- 【Java并发编程实战】-----“J.U.C”:CountDownlatch
上篇博文([Java并发编程实战]-----"J.U.C":CyclicBarrier)LZ介绍了CyclicBarrier.CyclicBarrier所描述的是"允许一 ...
- 【Java并发编程实战】-----“J.U.C”:CyclicBarrier
在上篇博客([Java并发编程实战]-----"J.U.C":Semaphore)中,LZ介绍了Semaphore,下面LZ介绍CyclicBarrier.在JDK API中是这么 ...
- 【Java并发编程实战】-----“J.U.C”:ReentrantReadWriteLock
ReentrantLock实现了标准的互斥操作,也就是说在某一时刻只有有一个线程持有锁.ReentrantLock采用这种独占的保守锁直接,在一定程度上减低了吞吐量.在这种情况下任何的"读/ ...
- JAVA并发编程J.U.C学习总结
前言 学习了一段时间J.U.C,打算做个小结,个人感觉总结还是非常重要,要不然总感觉知识点零零散散的. 有错误也欢迎指正,大家共同进步: 另外,转载请注明链接,写篇文章不容易啊,http://www. ...
- Android Studio解决未识别Java文件(出现红J)问题
1.问题:java文件出现了红J的问题,正常情况下应该是显示蓝色的C标识. 2.解决方案:切换到project视图下,找到app这个module里的build.gradle,在android结构里插入 ...
随机推荐
- 1. scrapy的安装
1.安装lxml pip install lxml 2.安装twisted 在https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted网站搜索twiste ...
- 【AUTO Uninstaller 中文版-详细使用教程】AUTODESK系列软件MAYA/CAD/3DSMAX/INVENTOR终极完美修复卸载工具 Windows x64位 【原创搬家】
小伙伴是不是遇到 MAYA/CAD/3DSMAX/INVENTOR/REVIT 安装失败或者安装不了的问题了呢?AUTODESK系列软件着实令人头疼,MAYA/CAD/3DSMAX/INVENTOR/ ...
- 模糊测试之AVI文件分析
本次试验主要是针对AVI的处理,了解AVI的基本概念,并且掌握AVI文件常用的程序读写方法.知道AVI视频文件的帧的读取方法,以及了解BMP和AVI的基本关系. 本文作者:i春秋签约作家——天天 一 ...
- h5博彩webapp项目实例|h5棋牌游戏|h5博彩app案例
html5实现的博彩webapp.h5棋牌app实例,运用h5+css3+zepto+jQ+swiper+layer等技术进行布控开发,750px最大宽度适配手机端设备,采用flex+rem布局样式. ...
- c# Xml反序列化示例
Xml 示例 <?xml version="1.0" encoding="utf-8"?> <CarCollection> <Ca ...
- 关于防止表单form重复提交的方式
表单重复提交: 1.第一种:添加以后刷新页面(刷新的是Servlet) 2.第二种:重复点击提交按钮. * 使用令牌机制:(防止表单重复提交) * 在表单页面中 生成一个令牌 * 将这个令牌保存在se ...
- 【jQuery源码】DOM Ready
一直以来,各种JS最佳实践都会告诉我们,将JS放在HTML的最后,即</body>之前,理由就是:JS会阻塞下载,而且,在JS中很有可能有对DOM的操作,放在HTML的最后,可以尽可能的保 ...
- 四则运算2及psp0设计
随机生成运算式,要求: 1.题目避免重复. 2.可定制(数量/打印方式). 3.可以控制一下参数. 要求:是否有乘除法,是否有括号,数值范围,加减有无负数,除法有无余数. 刚开始看到这样一个题目感觉还 ...
- 解决chrome,下载在文件夹中显示,调用错误的关联程序
https://blog.csdn.net/qq_32337527/article/details/81778732?utm_source=blogxgwz0
- 广州.NET俱乐部简介
广州.NET俱乐部 简介 广州 .NET 俱乐部自2018年底重新在广州地区活跃. 目前已经成功在广州.深圳.苏州和东莞组织并参与了多长线下技术交流活动. 已经有超过1200+人加入了俱乐部线上社区. ...