按白书上说的,先用一次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. swift pragma mark

    众所周知,大家在OC中对代码进行逻辑组织 用的是#pragma mark - ,生成分隔线 用#pragma mark 函数说明,来生成一个函数的说明X 但在swift中,这个语法就不支持了,毕竟它是 ...

  2. MySQL数据库各个版本的区别

    MySQL数据库各个版本的区别 MySQL数据库 MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),MySQL数据库系统使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管 ...

  3. HTTPie:一个不错的 HTTP 命令行客户端

    转自:http://top.jobbole.com/9682/ HTTPie:一个不错的 HTTP 命令行客户端 HTTPie (读aych-tee-tee-pie)是一个 HTTP 的命令行客户端. ...

  4. [luogu4053 JSOI2007] 建筑抢修 (贪心 优先队列)

    传送门 题目描述 小刚在玩JSOI提供的一个称之为"建筑抢修"的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有N个建筑设施受到了严重的损伤 ...

  5. CentOS 笔记(三) 目录结构

    理解CentOS 目录结构 首次登录进入,应该是进入了,run文件夹 通过  cd  ../ 进入了,最根节点 通过 ls  显示全部文件夹 通过 pwd 查看当前目录 参考: https://www ...

  6. 01.Python基础-4.字符串

    1 字符串初识 字符串编码 字符串定义:多个字符(双引号或单引号中的数据)组成 字符串下标 类似列表list 格式化 就是占位符 最常用%s %d %f等等 转义字符 转义字符 描述 \ (在行尾时) ...

  7. django-5-自定义模板过滤器及标签

    <<<代码布局(自定义的代码放哪里)>>> (1)某个app特有的  1.一般放app目录下 固定名为templatetags 的python文件夹里鸭,如果是别的 ...

  8. windows FTP自动下载脚本

    新建ftp.dat文件,内容如下 open xxx.xxx.xxx.xxxusernamepasswordcd    lcd x:\filesbinaryhashmget *.*bye 新建run.b ...

  9. BA-Siemens-时间表

    问题1:弹出了subsystem:atom Identifier 0000000023的错误对话框,此问题目前不知道如何处理,先攒着吧.

  10. ARP协议(3)ARP编程--winpcap&amp;vs2012配置

    好.之前说了那么多.最终到了,我们能够操刀的时候了. 在对ARP协议编程前.我们必需要能控制网络适配器(网卡).这个部分就是驱动! "我们要编写网卡驱动?",对,可是,至少我们现阶 ...