题目传送门

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. redis限流器的设计

    1.定义注解 import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java. ...

  2. 激活密钥许可证VMware Workstation Pro 15 激活许可证

    虚拟机 VMware Workstation Pro 15.5.0 及永久激活密钥 虚拟机下载地址:https://download3.vmware.com/software/wkst/file/VM ...

  3. Sass函数:Sass Maps的函数-map-get($map,$key)

    map-get($map,$key) 函数的作用是根据 $key 参数,返回 $key 在 $map 中对应的 value 值.如果 $key 不存在 $map中,将返回 null 值.此函数包括两个 ...

  4. Vue组件-组件组合

    组件设计初衷就是要配合使用的,最常见的就是形成父子组件的关系:组件 A 在它的模板中使用了组件 B. <html> <head> <title>Vue组件 A 在它 ...

  5. mysql 联合表查询从表即使有索引依然ALL的一个原因

    那就是主表和从表的关联字段的编码方式不一样!!! 晕啊,折腾了半天才发现,可能是不知道啥时候mysql更改主体编码方式了,结果导致后来新建的表的关联字段和之前的主表的字段的编码方式不一样 改成一样的编 ...

  6. Linux系统常用知识(centos7)

    一.查看系统版本 1.查看linux内核版本 #cat /etc/redhat-release 二.主机名 2.1定义: 静态的(Static hostname):“静态”主机名也称为内核主机名,是系 ...

  7. 批量定时任务将rtf文件转为docx,入参是rtf文件夹,生成一个docx文件夹

    java,python等语言对于rft的处理很受限,rtf提供了很少的api供外部调用处理,但是对于docx我们却又很多api来处理,所以很多人会有需求将rtf批量转为docx的需求,接下来就来说说解 ...

  8. javascript 回到顶部效果的实现

    demo.js window.onload=function() { var timer=null; var obtn=document.getElementById('btn'); var isTo ...

  9. SDOI2019R2翻车记

    额...貌似是学OI以来翻得最惨的一次比赛了呢... 不过还好是初三 但是没有机会和学长们打最后一场告别赛了呢(笑 按照惯例还是要记录一下吧. DAY ? 中考倒计时30天.来写这篇游记. DAY 0 ...

  10. 12.整合neo4j

    neo4j 官网下载: https://neo4j.com/download-center/#community 教程: http://neo4j.com.cn/public/cypher/defau ...