题目传送门

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. RabbitMQ ——简单队列

    一 .概述 我们不从开始就讲述基本的概念,尤其是在Rabbitmq之中有些概念确实比较难以理解,我们首先做的就是将光放提供的消息模型 进行实现,然后再总结一下Rabbitmq之中的基本概念. 二 .基 ...

  2. LuaLuaMemorySnapshotDump-master

    https://codeload.github.com/yaukeywang/LuaMemorySnapshotDump/zip/master

  3. sort - 对文本文件的行排序

    SYNOPSIS(总览) ../src/sort [OPTION]... [FILE]... DESCRIPTION(描述) ?谡舛砑尤魏胃郊拥拿枋鲂畔? 将排序好的所有文件串写到标准输出上. +P ...

  4. jenkins部署的零碎知识

    环境要求 1)版本控制子系统(SVN):SVN服务器.项目对应版本库.版本库中钩子程序(提交代码后,触发Jenkins自动打包并部署到应用服务器)(2)持续集成子系统(存在Jenkins的服务器):J ...

  5. Git--07 Gitlab备份与恢复

    目录 Gitlab备份与恢复 01). 备份 02). 恢复 Gitlab备份与恢复 ​ 对gitlab进行备份将会创建一个包含所有库和附件的归档文件.对备份的恢复只能恢复到与备份时的gitlab相同 ...

  6. JDBC之Statement、PreparedStatement和CallableStatement

    JDBC提供了Statement.PreparedStatement和CallableStatement三种方式来执行查询语句,其中Statement用于通用查询,PreparedStatement用 ...

  7. Oracle 反键索引/反向索引

    反键索引又叫反向索引,不是用来加速数据访问的,而是为了均衡IO,解决热块而设计的比如数据这样: 1000001 1000002 1000005 1000006 在普通索引中会出现在一个叶子上,如果部门 ...

  8. .net core 操作oracle

    依赖项——右键——管理NuGet程序包——浏览——输入以下内容 oracle.ManagedDataAccess.core(记得勾选包括预发行版) 在页面中加入操作数据库的方法 public IAct ...

  9. AGC016题解

    呼我竟然真的去刷了016QwQ[本来以为就是个flag的233] 感觉AGC题目写起来都不是很麻烦但是确实动脑子qvq[比较适合训练我这种没脑子选手] 先扔个传送门:点我 A.Shrinking 题意 ...

  10. python基础知识1

    1.何为json? json 是一种轻量级的数据交换格式,采用完全独立于编程语言的文本格式来存储和表示数据.简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言. 易于人阅读和编写,同时也易于机 ...