题目传送门

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

题意:题目很清晰,就是代号J要逃离迷宫,但是在迷宫的一些部分有一些火(fire)会蔓延开来,让你求出最短逃离时间,或者输出IMPOSSIBLE

本来解法我都想到了,就是在有火的地方bfs,计算它蔓延到每个地方的时间,然后人再bfs计算出可行的路径,这里有一个坑就是,火不一定只有一个,在文中是用“portions”,注意这里使用复数.对,这里我没注意到,我一开始还提交了8遍CE,提交错了语言,都是泪啊!!最后改过来后,在提交几次WA后我发现我bfs的结束把m写成了n,无语了………

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define INF 0x3f3f3f3f
#define MAX 1005
int n,m;
int sx,sy,fx,fy;
char g[MAX][MAX];
bool vis[MAX][MAX];
int fire[MAX][MAX];
int ans=INF;
int dx[]={,,,-},dy[]={,,-,};
struct mask
{
int x,y,step;
mask(){}
mask(int xx,int yy,int st)
{
x=xx,y=yy,step=st;
}
};
struct fir
{
int x,y,time;
fir(){}
fir(int xx,int yy,int ti)
{
x=xx,y=yy,time=ti;
}
};
queue<mask>q;
queue<fir>fi;
bool check(int a,int b)
{
return <=a&&a<n&&<=b&&b<m&&g[a][b]!='#';
}
//遍历火的蔓延速度
void bfs_fire()
{
while(fi.size())
{
fir tmp=fi.front();fi.pop();
for(int i=;i<;i++)
{
int nx=tmp.x+dx[i];
int ny=tmp.y+dy[i];
if(fire[nx][ny]>tmp.time+&&check(nx,ny))
{//cout<<"ok"<<endl;
fire[nx][ny]=min(fire[nx][ny],tmp.time+);
fi.push(fir(nx,ny,tmp.time+));
}
}
}
}
//遍历人的可行路径
int bfs()
{
memset(vis,false,sizeof(vis));
while(q.size())q.pop();
vis[sx][sy]=true;
q.push(mask(sx,sy,));
while(q.size())
{
mask tmp=q.front();q.pop();
if(tmp.x==n-||tmp.y==m-||tmp.x==||tmp.y==)
{
ans=min(ans,tmp.step);
}
for(int i=;i<;i++)
{
int nx=tmp.x+dx[i];
int ny=tmp.y+dy[i];
if(check(nx,ny)&&tmp.step+<fire[nx][ny]&&!vis[nx][ny])
{
vis[nx][ny]=true;
q.push(mask(nx,ny,tmp.step+));
}
}
}
return ans==INF?-:ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
while(fi.size())fi.pop();
memset(fire,INF,sizeof(fire));
for(int i=;i<n;i++)
{
scanf("%s",&g[i]);
for(int j=;j<m;j++)
{
if(g[i][j]=='J')
{
sx=i,sy=j;
}
if(g[i][j]=='F')
{
fire[i][j]=;//注意这里的火可能不止一个,所以要全部加入
fi.push(fir(i,j,));
}
}
}
ans=INF;
bfs_fire();
/* for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
cout<<fire[i][j]<<" ";
cout<<endl;
}*/
int d=bfs();
if(d==-)
printf("IMPOSSIBLE\n");
else printf("%d\n",d+);
} return ;
}

UVA - 11624 J - Fire! (BFS)的更多相关文章

  1. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  2. CJOJ 1071 【Uva】硬币问题(动态规划)

    CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...

  3. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  4. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  5. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  6. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  7. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  8. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  9. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

随机推荐

  1. React中异步模块api React.lazy和React.Suspense

    React.lazy React.lazy 这个函数需要动态调用 import().它必须返回一个 Promise,该 Promise 需要 resolve 一个 defalut export 的 R ...

  2. sendmail 出现 My unqualified host name的解决办法

    有"My unqualified host name"错误 修改/etc/hosts, 在本机的ip那一行, 在xxxhostname后面加上"  xxxhostname ...

  3. 再谈lmbench

    摸了一轮ltp-ddt 再回头来看lmbench bandwidth & latency合集小王子 用起来确实方便. 只是官网显示的用法是: Go to the top directory, ...

  4. grep正则 以.o结尾的文件

    ls -l | grep *.o 查不出任何东西 . 代表一定有一个任意字符 * 重复零个到无穷多个前一个字符(所以需要前面有字符) 所以应该是 ls -l | grep '.*\.o' .*表示零个 ...

  5. BZOJ2213 & LOJ2161 「POI2011 R2 Day1」Difference 最大子段和

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2213 https://loj.ac/problem/2161 题解 做一道简单题来放松一下. ...

  6. Es学习第八课, Filter、bool和范围查询

    Filter过滤查询 filter是不计算相关性的,同时可以缓存.因此filter速度快于query. 我们先在kibana上先添加数据来做准备 POST /lib4/items/_bulk { &q ...

  7. SQL执行顺序和coalesce以及case when的用法

    1.mysql的执行顺序 from on join where group by having select distinct union   //UNION 操作符用于合并两个或多个 SELECT ...

  8. matlab中求解线性方程组的rref函数

    摘自:http://www.maybe520.net/blog/987/ matlab中怎么求解线性方程组呢? matlab中求解线性方程组可应用克拉默法则(Cramer's Rule)即通过det( ...

  9. 修改host,访问指定服务器

    途径: 1.hosts文件修改 以Windows文件为例:进入system32/drivers/etc/hosts 或者用一些软件比如switchhost等来进行修改 2.通过抓包工具修改 因为本人是 ...

  10. BLUEHOST香港主机FTP连接不上解决办法

    昨天购买了BLUEHOST的香港主机后,以为一切顺风顺水,结果FTP却连接不上,用了多种FTP工具都不行,​​​按官方博客要求开启了TSL仍然不行​.​经过一晚上的测试后终于成功,现分享出来. 方法一 ...