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 ...
随机推荐
- js prototype 原型
//https://xxxgitone.github.io/2017/06/10/JavaScript%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E4% ...
- win 运行
1.msconfig - 系统配置 - 服务-全部禁用 2.DXDIAG direct版本
- linux chattr用法
在linux中,我们有的时候发现linux无法删除一个文件或者目录. huskiesir第一次遇见这个问题还是在一次服务器被不法分子入侵之后的事情,我就发现某个进程很多,根据进程的名字,我搜索关键字找 ...
- 搞定PHP面试 - 运算符知识点整理
一.算术运算符 1. 概览 例子 名称 结果 $a + $b 加法 $a 和 $b 的和. $a - $b 减法 $a 和 $b 的差. $a * $b 乘法 $a 和 $b 的积. $a / $b ...
- php 返回某个月的 每周几有几天
不得不承认,这真是一个奇葩的需求,无奈写个类凑活用用. 输入日期格式或者 时间戳,返回当月有多少个周一.周二.周三.....周日; 思路就是 找到这个月有多少天,在便利判断. 稍微考虑下闰年的情况 前 ...
- C#中的Attribute定义及用法
1.Attribute定义 公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法和属性等.Attributes和Microsoft .N ...
- 计算机-award BIOS全教程
- spring boot的几种配置类型
1.spring boot的几种配置类型 1)基本配置,spring自动读取的,全都在application.yml里配置,spring会自动读取这个配置文件 2)个性化配置:比如配置intercep ...
- [SharePoint][SharePoint Designer 入门经典]Chapter13 客户端JavaScript编程
1.创建客户对象模型的页面 2.使用CAML从SPS中取得数据 3.创建更新删除列表项目 4.为ribbon添加项目
- poj1035Spell checker
暴力解决. 先把字典里的每一个单词的长度存起来.在查找的时候.就比較长度,在多一个少一个之间找, #include<stdio.h> #include<string.h> #i ...