uva 11624 Fire! 【 BFS 】
按白书上说的,先用一次bfs,求出每个点起火的时间
再bfs一次求出是否能够走出迷宫
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; const int maxn = ;
const int INF = << -;
int r,c;//r行,c列
int d[maxn][maxn];
int vis[maxn][maxn];
char g[maxn][maxn];
int sx,sy; int dir[][] = {,,-,,,,,-}; struct node{
int x,y;
int step;
}p[maxn]; queue<node> Q; void bfs1(){
memset(vis,,sizeof(vis));
while(!Q.empty()){
node v = Q.front();Q.pop();
for(int i = ;i < ;i++){
int xx = v.x + dir[i][];
int yy = v.y + dir[i][];
if(xx < || xx > r || yy < || yy > c || vis[xx][yy] || g[xx][yy] != '.') continue;
vis[xx][yy] = ;
d[xx][yy] = min(d[xx][yy],v.step+);
Q.push(node{xx,yy,v.step+});
}
}
} void bfs2(){
queue<node> q;
memset(vis,,sizeof(vis));
q.push(node{sx,sy,});vis[sx][sy] = ; while(!q.empty()){
node u = q.front();q.pop();
if(u.x == r || u.y == c || u.x == || u.y == ){
printf("%d\n",u.step + );
return;
}
for(int i = ;i < ;i++){
int xx = u.x + dir[i][];
int yy = u.y + dir[i][];
if(xx < || xx > r || yy < || yy > c || vis[xx][yy] || g[xx][yy] != '.') continue;
if(u.step + >= d[xx][yy]) continue;
vis[xx][yy] = ;
q.push(node{xx,yy,u.step+});
}
}
puts("IMPOSSIBLE");
} int main(){
int T;
scanf("%d",&T);
while(T--){
while(!Q.empty()) Q.pop();
scanf("%d %d",&r,&c);
for(int i = ;i <= r;i++){
for(int j = ;j <= c;j++) d[i][j] = INF;
}
for(int i = ;i <= r;i++){
for(int j = ;j <= c;j++) {
cin>>g[i][j];
if(g[i][j] == 'F') Q.push(node{i,j,});
if(g[i][j] == 'J') sx = i,sy = j;
}
}
bfs1();
bfs2();
}
return ;
}
加油~~~gooooooo~~
uva 11624 Fire! 【 BFS 】的更多相关文章
- UVA - 11624 Fire! 【BFS】
题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- (简单) UVA 11624 Fire! ,BFS。
Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...
- UVA - 11624 Fire! 双向BFS追击问题
Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...
- UVA_11624 Fire! 【BFS】
一.题面 略 二.题意分析 一个迷宫中,有一个人Joe和一个或多个起火点,起火点可以蔓延,人可以走动,都只能走4个方向,问人能走出去的最少步数,如果不能输出不可能.很多大佬说是两遍BFS,先一遍火,记 ...
- 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】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
随机推荐
- C# 生成Model和DAL
using Model; using System.Collections.Generic; using System.Text; public class Class1 { #region 生成Mo ...
- redis RDB快照和AOF日志持久化配置
Redis持久化配置 Redis的持久化有2种方式 1快照 2是日志 Rdb快照的配置选项: save 900 1 // 900内,有1条写入,则产生快照 save 300 1000 ...
- Python笔记18-----函数收集参数
1.收集参数(参数前面加*): def test1(param1,*params): print(param1) print(params) 调用:test1(1,2,3,4) 结果:1 (2,3,4 ...
- Django生命周期,FBV,CBV
一. Django生命周期 首先我们知道HTTP请求及服务端响应中传输的所有数据都是字符串,在Django中,当我们访问一个的url时,会通过路由匹配进入相应的html网页中.Django的请求生命周 ...
- [luogu3203 HNOI2010] 弹飞绵羊 (分块)
传送门 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...
- FFMpeg 常用命令格式转换,视频合成
FFmpeg都是命令行的,用起来肯定不方便.但是,这对技术宅应该不成问题.下面,我就罗列一些比较实用的使用方法吧. FFmpeg的下载与安装 FFmpeg是开源的.但我们不必去下载它的源代码.下载已经 ...
- Python中的全局变量与局部变量的区别
全局变量与局部变量两者的本质区别就是在于作用域 用通俗的话来理解的话, 全局变量是在整个py文件中声明,全局范围内都可以访问 局部变量是在某个函数中声明的,只能在该函数中调用它,如果试图在超出范围的地 ...
- XML文件基础
https://c3d.club/xml/basic/2018/12/27/xml-file-base.html 1.文件头 XML文件头有XML声明与DTD文件类型声明组成.其中DTD文件类型声 ...
- Java Web学习总结(27)——JavaEE中Web服务器、Web容器、Application服务器区别及联系
在JavaEE 开发Web中,我们经常会听到Web服务器(Web Server).Web容器(Web Container).应用服务器(Application Server),等容易混淆不好理解名词. ...
- 0111mysql如何选择Join的顺序
本文通过一个案例来看看MySQL优化器如何选择索引和JOIN顺序.表结构和数据准备参考本文最后部分"测试环境".这里主要介绍MySQL优化器的主要执行流程,而不是介绍一个优化器的各 ...