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)的更多相关文章

  1. uva11624 - Fire!

    uva11624 - Fire! 火在蔓延,人在走.火会蔓延,不会熄灭,我们可以确定某个点着火的时间(广搜).对于J来说,要是他走到某点的时间比火蔓延到该点的时间要短,那么他走到该点的时候,火还没蔓延 ...

  2. UVA11624 Fire! —— BFS

    题目链接:https://vjudge.net/problem/UVA-11624 题解: 坑点:“portions of the maze havecaught on fire”, 表明了起火点不唯 ...

  3. uva11624 Fire! (bfs预处理)

    题目链接:https://vjudge.net/problem/UVA-11624 题意:给一个1000×1000的矩阵,有几个着火点和Joe,着火点和Joe每个单位时间均移动一个单位,求Joe逃出的 ...

  4. 【Java并发编程实战】-----“J.U.C”:CAS操作

    CAS,即Compare and Swap,中文翻译为"比较并交换". 对于JUC包中,CAS理论是实现整个java并发包的基石.从整体来看,concurrent包的实现示意图如下 ...

  5. 【Java并发编程实战】-----“J.U.C”:Exchanger

    前面介绍了三个同步辅助类:CyclicBarrier.Barrier.Phaser,这篇博客介绍最后一个:Exchanger.JDK API是这样介绍的:可以在对中对元素进行配对和交换的线程的同步点. ...

  6. 【Java并发编程实战】-----“J.U.C”:CountDownlatch

    上篇博文([Java并发编程实战]-----"J.U.C":CyclicBarrier)LZ介绍了CyclicBarrier.CyclicBarrier所描述的是"允许一 ...

  7. 【Java并发编程实战】-----“J.U.C”:CyclicBarrier

    在上篇博客([Java并发编程实战]-----"J.U.C":Semaphore)中,LZ介绍了Semaphore,下面LZ介绍CyclicBarrier.在JDK API中是这么 ...

  8. 【Java并发编程实战】-----“J.U.C”:ReentrantReadWriteLock

    ReentrantLock实现了标准的互斥操作,也就是说在某一时刻只有有一个线程持有锁.ReentrantLock采用这种独占的保守锁直接,在一定程度上减低了吞吐量.在这种情况下任何的"读/ ...

  9. JAVA并发编程J.U.C学习总结

    前言 学习了一段时间J.U.C,打算做个小结,个人感觉总结还是非常重要,要不然总感觉知识点零零散散的. 有错误也欢迎指正,大家共同进步: 另外,转载请注明链接,写篇文章不容易啊,http://www. ...

  10. Android Studio解决未识别Java文件(出现红J)问题

    1.问题:java文件出现了红J的问题,正常情况下应该是显示蓝色的C标识. 2.解决方案:切换到project视图下,找到app这个module里的build.gradle,在android结构里插入 ...

随机推荐

  1. Bootstrap框架(二)

    day58 巨幕 这是一个轻量.灵活的组件,它能延伸至整个浏览器视口来展示网站上的关键内容. Hello, world! This is a simple hero unit, a simple ju ...

  2. 鬼知道是啥系列之——STL(lower_bound(),upper_bound() )

    引子,不明觉厉:   百度,渐入佳境: 头铁,入门到放弃: lower_bound(): 头文件:  #include<algorithm>函数功能:  函数lower_bound()在f ...

  3. windows系统上安装与使用Android NDK r5

    windows系统上安装与使用Android NDK r5  很早就听说了android的NDK应用,只是一直没有时间去研究,今天花了点时间在windows平台搭建了NDK环境,并成功运行了第一个简单 ...

  4. Advanced Debugging and the Address Sanitizer

    A debug trick 在异常端点处运行 po $arg1,找出异常信息.  Address Sanitizer 概述 是一个运行时检测工具 发现内存问题 可以用于模拟器和设备 可以发现的问题 ...

  5. GDOI2018滚粗记

    day-50: 高中全体成员去了北京训练,我被虐成傻逼(貌似总分全校倒数第2). day-20: 回广州了,间断式略微考好55555..... day0: 早上起床好像有点晚qwq 然后简单打了个FF ...

  6. POJ 1243

    #include <iostream> #include <algorithm> #include <cmath> #define MAXN 50 using na ...

  7. QuantLib 金融计算——修复 BatesProcess 中的两个 Bug

    QuantLib 金融计算--修复 BatesProcess 中的两个 Bug 我发现了 BatesProcess 中的两个 Bug: 基类 HestonProcess::factors 的返回值取决 ...

  8. 有关Hadoop的六大误解

    迄今为止,Hadoop和大数据实际上是同义词.然而随着大数据的炒作不断升温,出现了很多对Hadoop如何应用于大数据的误解. Hadoop是一种用于存储和分析大型数据集开源软件框架,可处理分布在多个现 ...

  9. Spring Security构建Rest服务-0100-前言

    一.我的前言 这是看慕课网老师讲的SpringSecurity的学习笔记,老师讲的很好,开篇就说到了我的心里,老师说道: 有一定经验的程序员如何提升自己? 1,每天都很忙,但是感觉水平没有提升 2,不 ...

  10. OpenGL12-shader(GLSL)着色语言2-(参数传递)(代码以上传)

    上一篇中介绍了如何使用shader,用来一个最简单的shader,计算顶点的位置,调用了 OpenGL 顶点着色语言中的内置变量对顶点进行操作,这一例程中,将展示如何将应用层 的数据传递到shader ...