按白书上说的,先用一次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 】的更多相关文章

  1. UVA - 11624 Fire! 【BFS】

    题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...

  2. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  3. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  4. (简单) UVA 11624 Fire! ,BFS。

    Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...

  5. UVA - 11624 Fire! 双向BFS追击问题

    Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...

  6. UVA_11624 Fire! 【BFS】

    一.题面 略 二.题意分析 一个迷宫中,有一个人Joe和一个或多个起火点,起火点可以蔓延,人可以走动,都只能走4个方向,问人能走出去的最少步数,如果不能输出不可能.很多大佬说是两遍BFS,先一遍火,记 ...

  7. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  8. UVa 11624 Fire!(着火了!)

    UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...

  9. UVA 11624 Fire!【两点BFS】

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...

随机推荐

  1. Lazy Initialization with Swift

    Lazy initialization (also sometimes called lazy instantiation, or lazy loading) is a technique for d ...

  2. Linux 内核剖解(转)

    Linux 内核剖析(转)  linux内核是一个庞大而复杂的操作系统的核心,不过尽管庞大,但是却采用子系统和分层的概念很好地进行了组织.在本文中,您将探索 Linux 内核的总体结构,并学习一些主要 ...

  3. spring helloword

    控制反转: Inversion on Control , 控制反转 IOC 对象的创建交给外部容器完成,这个就做控制反转. 依赖注入,  dependency injection 处理对象的依赖关系 ...

  4. 取/etc/password文件最后一个单词的最后一个字符

    三种方法都可以 [root@localhost ~]# sed -n "1,5 s#.*\(.\)#\1#p" /etc/passwd [root@localhost ~]# se ...

  5. Bonjour/Zeroconf with Arduino

    转自:http://gkaindl.com/software/arduino-ethernet/bonjour Bonjour/Zeroconf with Arduino DownloadVersio ...

  6. PHP开发过程中数组汇总 [ 持续更新系列 ]

    开发过程中经常会使用到数组函数,故特地总结出来,自己熟悉,同时供大家参考!(实例部分会抽空尽快完成) 一.目录 array_merge(); 合并数组 array_keys(); array_filt ...

  7. 关于python从Oracle中读取数据中文全是问号的问题

    import os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' 问题搞定

  8. @Bean 指定初始化和销毁方法

    bean 的生命周期 bean 的创建 --> 初始化 --> 销毁 ioc 容器管理 bean 的声明周期 可以自定义初始化和销毁方法 构造器( 对象创建 )被调用时机 单实例:在容器启 ...

  9. HDU 2857 Mirror and Light

    /* hdu 2857 Mirror and Light 计算几何 镜面反射 */ #include<stdio.h> #include<string.h> #include& ...

  10. idea debug打得断点第一次可以进入,第二次不能进入