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) ...
随机推荐
- 求解:为什么impala实现hive查询 可以使用ifnull()函数,不可以使用length() 函数
求大神解惑,找了很久都没有找到为什么??? hive支持length() 函数,不支持ifnull()函数??? impala实现hive查询 支持ifnull()函数,不支持length() 函数 ...
- vue 实现模糊检索,并根据其他字符的首字母顺序排列
昨天让我做一个功能,实现一个模糊检索,我就想,那做呗,然后开始正常的开发 代码如下: HTML VUE 因为是实时的,所以写了将逻辑写到了watch中 五分钟搞定. 我以为这就完了,然而产品的需求 ...
- elasticsearch 深入 —— normalizer
keyword字段的normalizer属性类似于分析器,只是它保证分析链生成单个token. 在索引关键字之前,以及在通过诸如match查询之类的查询解析器或者通过诸如term查询之类的术语级查询搜 ...
- GeneXus笔记本—常用函数(下)
这篇是常用函数的最后一节 当然 我这里聊的还不是全部的,需要各位朋友继续在工作中去深入才行啊 ,毕竟从入门到入土....┌(; ̄◇ ̄)┘ 1:Sleep 这个函数你们应该能猜到 ”To allow m ...
- 使用transporter同步MongoDB数据到es
由于logstash不支持MongoDB自定义主键导入es,所以使用transporter导入数据. 版本:es5.x,transporter0.25, es6以上不允许一个索引下面多个type,tr ...
- 初学Java 使用输入对话框
import javax.swing.JOptionPane; public class ComputeLoanUsingInputDialog { public static void main(S ...
- jsp中引入js文件缓存问题解决
加上版本UUID <script charset="utf-8" src="${basePath}js/souke/soukegalist.js?v=<%=U ...
- 【串线篇】Mybatis之SSM整合
SSM:Spring+SpringMVC+MyBatis 建立Java web项目 一.导包 1).Spring: [aop核心] com.springsource.net.sf.cglib-2.2. ...
- Java字符串流学习
字符串流 定义:字符串流,以一个字符为数据源,来构造一个字符流. 作用:在Web开发中,我们经常要从服务器上获取数据,数据返回的格式通过一个字符串(XML.JSON),我们需要把这个字符串构造为一个字 ...
- python基础:8.正则表达式
1.概念 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. re模块的常见方法: ...