UVA 11624 Fire!(两次BFS+记录最小着火时间)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671
题目大意:一个n*m迷宫,迷宫中有一个点'J'是人也就是起点,有多个点'F'表示着火,人每秒都可以往水平或垂直方向走,火势也可以往水平或垂直方向蔓延,火和人都不能过'#',但可以过‘.’。问人最后能否从迷宫逃出,不被火烧到。
解题思路:两次bfs,第一次求各点被火烧到的最小时间用step[x][y]记录,如果烧不到就是inf。第二次bfs,求从起点能否逃出迷宫,但是比如说下一步走到(x,y)时间是t,t如果>=step[x][y]则这个点不能走,因为走到这里时,这里已经备火势蔓延了。注意:有多个着火点,看了我两个小时。气死我了啊啊啊!!!
代码:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=1e3+;
const int inf=0x3f3f3f3f; struct node{
int x,y,step;
}pre,now; int m,n,ans,cnt,x1,y1;
int d[][]={{,},{,},{-,},{,-}};
char map[N][N];
int vis[N][N],step[N][N];//step[x][y]记录被各点烧到的最短时间 struct node2{
int x,y;
}point[N*N]; //求着火时间
void bfs1(){
queue<node>q;
for(int i=;i<=cnt;i++){
int x=point[i].x;
int y=point[i].y;
now.x=x;
now.y=y;
now.step=;
step[x][y]=;
q.push(now);
}
while(!q.empty()){
pre=q.front();
q.pop();
for(int i=;i<;i++){
int xx=pre.x+d[i][];
int yy=pre.y+d[i][];
int t=pre.step+;
if(xx<||yy<||xx>n||yy>m||map[xx][yy]=='#'||step[xx][yy]!=inf)
continue;
step[xx][yy]=t;
now.x=xx;
now.y=yy;
now.step=t;
q.push(now);
}
}
}
//求是否能逃出去
bool bfs2(){
queue<node>q;
now.x=x1;
now.y=y1;
now.step=;
q.push(now);
while(!q.empty()){
pre=q.front();
q.pop();
for(int i=;i<;i++){
int xx=pre.x+d[i][];
int yy=pre.y+d[i][];
int t=pre.step+;
if(t>=step[xx][yy]||map[xx][yy]=='#'||vis[xx][yy])
continue;
if(xx<||yy<||xx>n||yy>m){
ans=t;
return true;
}
vis[xx][yy]=;
now.x=xx;
now.y=yy;
now.step=t;
q.push(now);
}
}
return false;
} int main(){
int T;
scanf("%d",&T);
while(T--){
cnt=;
memset(vis,,sizeof(vis));
memset(step,0x3f,sizeof(step));
memset(map,'.',sizeof(map));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
getchar();
for(int j=;j<=m;j++){
scanf("%c",&map[i][j]);
if(map[i][j]=='F'){
point[++cnt].x=i;
point[cnt].y=j;
}
if(map[i][j]=='J')
x1=i,y1=j;
}
}
bfs1();
if(!bfs2())
puts("IMPOSSIBLE");
else
printf("%d\n",ans);
}
return ;
}
UVA 11624 Fire!(两次BFS+记录最小着火时间)的更多相关文章
- Fire! UVA - 11624 (两步bfs)
题目链接 题意 人要从迷宫走出去,火会向四个方向同时扩散 分析 两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能 ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...
- UVA 11624 Fire!(二次BFS)
先对火BFS一次,求出每个点的最小着火时间. 再对人BFS一次,求出走到边界的最少时间. #include <iostream> #include <queue> #inclu ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA - 11624 Fire! 【BFS】
题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...
- UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
随机推荐
- 【BZOJ2780】【SPOJ】Sevenk Love Oimaster(后缀自动机)
[BZOJ2780][SPOJ]Sevenk Love Oimaster(后缀自动机) 题面 BZOJ 洛谷 题解 裸的广义后缀自动机??? 建立广义后缀自动机建立出来之后算一下每个节点被几个串给包括 ...
- 【HDU4336】Card Collector (动态规划,数学期望)
[HDU4336]Card Collector (动态规划,数学期望) 题面 Vjudge 题解 设\(f[i]\)表示状态\(i\)到达目标状态的期望 \(f[i]=(\sum f[j]*p[j]+ ...
- Classical Binary Search
Find any position of a target number in a sorted array. Return -1 if target does not exist. 与题目 Firs ...
- c++常量详解
概念 常量是存放固定且不可变值的,一旦确定初始值则在程序其它地方不可改变, 所以const对象必须初始化.常量一般使用const关键字来修饰. const 对象可以大致分为三类: 1. const i ...
- 前端PHP入门-030-文件函数API
bool file_exists ( $指定文件名或者文件路径) 功能:文件是否存在. bool is_readable ( $指定文件名或者文件路径) 功能:文件是否可读 bool is_write ...
- JS笔记-强化版2
1.DOM: DOM : Document Object Model 文档对象模型 文档:html页面 文档对象:页面中元素 文档对象模型:定义 为了能够让程序(js)去操作页面中的元素 DO ...
- css之display:inline-block布局--转
css之使用display:inline-block来布局 css之display:inline-block布局 1.解释一下display的几个常用的属性值,inline , block, in ...
- Spring 与 SpringMVC 容器父子关系引出的相应问题
1)关系说明 spring 与 springmvc 父子关系:spring (父容器),springmvc (子容器) springmvc(子)--- 可调用 --> spring(父) 中的 ...
- 也谈matlab中读取视频的一个重要函数mmreader
也谈matlab中读取视频的一个重要函数mmreader 在matlab中输入help mmreader来查阅一下该函数,有如下信息: MMREADER Create a multimedia rea ...
- 重构改善既有代码设计--重构手法18:Self Encapsulate Field (自封装字段)
你直接访问一个值域(field),但与值域之间的耦合关系逐渐变得笨拙. 为这个值域建立取值/设值函数(getting/setting methods),并且只以这些函数来访问值域. private i ...