UVA - 11624 J - Fire! (BFS)
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)的更多相关文章
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- CJOJ 1071 【Uva】硬币问题(动态规划)
CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
随机推荐
- Vue PC端图片预览插件
*手上的项目刚刚搞完了,记录一下项目中遇到的问题,留做笔记: 需求: 在项目中,需要展示用户上传的一些图片,我从后台接口拿到图片url后放在页面上展示,因为被图片我设置了宽度限制(150px),所以图 ...
- SELECT INTO - 从一个查询的结果中创建一个新表
SYNOPSIS SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ] * | expression [ AS output_name ] [ ...
- Linux学习笔记之档案权限与目录配置
一. 档案权限与目录配置用户的属性信息: /etc/passwd用户的密码信息: /etc/shadow组的信息: /etc/group 每个用户都有唯一的UID供系统识别sudo -i 输入 ...
- Helm安装服务端tiller出现的问题
一.首先,我是看尚硅谷视频跟着操作出现了问题,视频链接:https://www.bilibili.com/video/av66617940/?p=58 再说下大概的部署流程 Helm 部署 Helm ...
- Vue:替换/合并现有的特性
假设这是 bs-date-input 的模板: <input type="date" class="form-control"> 为了给该日期选择器 ...
- 左上角小猫猫直达博主GitHub \-_-/
GitHub上有博主代码工程学习笔记啥的,由于推送比较方便所以有些学习笔记就没有上传到博客园
- RIME-使用小心得
从4月份我才接触到RIME输入法,当时的感觉上相见恨晚的,现在感觉也不错,时至今日,想写点东西,也算是小感触吧. RIME称为中州韵输入法引擎,是开放源代码的输入法软件,其主页是http://rime ...
- JVM---汇编指令集
<JVM指令助记符> 变量到操作数栈:iload,iload_,lload,lload_,fload,fload_,dload,dload_,aload,aload_ 操作数栈到变量:is ...
- 【leetcode】1023. Camelcase Matching
题目如下: A query word matches a given pattern if we can insert lowercaseletters to the pattern word so ...
- Linux系统重要文件
Linux系统重要文件介绍与配置 一网卡配置文件 (一)网卡配置文件参数介绍 文件路径信息:/ec/sysconfig/network-scripts/ifcfg-eth0 文件作用说明:配置网卡地址 ...