题目链接:http://vjudge.net/contest/132239#problem/A

题目链接:https://uva.onlinejudge.org/external/116/11624.pdf

《训练指南》P307

分析:只需要预处理每个格子起火的时间,在BFS扩展节点的时候加一个判断,到达该节点的时候,格子没有起火。

写法很巧妙,两次BFS类似,数据加一维kind,表示Joe到达该点和火到达该点。

#include <bits/stdc++.h>

using namespace std;

const int INF = 0x3f3f3f3f;
const int maxr = +;
const int maxc = +; int R,C;
char maze[maxr][maxc];
struct Cell{
int r,c;
Cell (int r,int c) : r(r),c(c) {}
};
const int dr[] = {-,,,};
const int dc[] = {,,-,};
int d[maxr][maxc][] ,vis[maxr][maxc][]; queue<Cell> Q;
void bfs(int kind)
{
while(!Q.empty())
{
Cell cell = Q.front();
Q.pop();
int r = cell.r, c = cell.c;
for(int dir = ; dir < ; dir++)
{
int nr = r + dr[dir], nc = c + dc[dir];
if(nr >= && nr < R && nc >= && nc < C && maze[nr][nc] == '.' && !vis[nr][nc][kind])
{
Q.push(Cell(nr, nc));
vis[nr][nc][kind] = ;
d[nr][nc][kind] = d[r][c][kind] + ;
}
}
}
} int ans;
void check(int r,int c)
{
if(maze[r][c]!='.'||!vis[r][c][]) return; ///走不到
if(!vis[r][c][]||d[r][c][]<d[r][c][]) ans= min(ans,d[r][c][]+); ///该点火到达不了,或者joe先于火
} int main()
{
//freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&R,&C);
int jr,jc;
vector<Cell> fires;
for(int i=;i<R;i++)
{
scanf("%s",maze[i]);
for(int j=;j<C;j++)
{
if(maze[i][j]=='J')
{
jr = i;
jc = j;
maze[i][j] = '.';
}
else if(maze[i][j]=='F')
{
fires.push_back(Cell(i,j));
maze[i][j] = '.';
}
}
}
memset(vis,,sizeof(vis)); ///各个点到joe的距离
///Joe
vis[jr][jc][] = ;
d[jr][jc][] = ;
Q.push(Cell(jr,jc));
bfs(); for(int i = ;i<fires.size();i++)
{
vis[fires[i].r][fires[i].c][] = ;
d[fires[i].r][fires[i].c][] = ;
Q.push(fires[i]);
}
bfs(); ans = INF;
for(int i=;i<R;i++)
{
check(i,);
check(i,C-);
}
for(int i=;i<C;i++)
{
check(,i);
check(R-,i);
}
if(ans==INF) printf("IMPOSSIBLE\n");
else printf("%d\n",ans);
}
return ;
}

UVa 11624,两次BFS的更多相关文章

  1. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

  2. UVa 11624 Fire!(BFS)

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

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

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

  4. UVA 11624 - Fire! 图BFS

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

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

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

  6. UVA - 11624 Fire! 【BFS】

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

  7. uva 11624 Fire! 【 BFS 】

    按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 #include<cstdio> #include<cstring> #include&l ...

  8. UVA 11624 Fire!(两次BFS+记录最小着火时间)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. Fire! UVA - 11624 (两步bfs)

    题目链接 题意 人要从迷宫走出去,火会向四个方向同时扩散 分析 两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能 ...

随机推荐

  1. Swift实战-QQ在线音乐(AppleWatch版)

    1.打开项目QQMusic,然后点菜单:“File-New-Target”添加appleWatch扩展项 2.选择Swift语言,把Include Notification Scene前的勾去掉 (项 ...

  2. UEditor和CKEditor配置上传图片,视频,附件

    UEditor: http://blog.sina.com.cn/s/blog_8bb128230102v12x.html CKEditor:http://blog.sina.com.cn/s/blo ...

  3. Linux的set

    功能说明: 设置shell 语 法: set [+-abCdefhHklmnpPtuvx] 补充说明: set指令能设置所使用shell的执行方式,可依照不同的需求来做设置. 参 数: -a 标示已修 ...

  4. demo14

    /Users/alamps/AndroidStudioProjects/Demo12SimpleAdapter/Demo12SimpleAdapter/src/main/res/layout/tabl ...

  5. 夺命雷公狗---DEDECMS----27dedecms电影的下载地址的完成

    我们现在要完成的是电影的下载地址这里: 我们的下载地址都是放在我们的dede_addonmovie(附加表)里面去才可以的,因为下载地址的个数是不能确定的,所以我们就将所有的下载地址存放到一个字段里面 ...

  6. 动态时间规整(DTW) 转载

    Dynamic Time Warping(DTW)诞生有一定的历史了(日本学者Itakura提出),它出现的目的也比较单纯,是一种衡量两个长度不同的时间序列的相似度的方法.应用也比较广,主要是在模板匹 ...

  7. python pdb

    pdb 以参数-m pdb启动后,pdb定位到下一步要执行的代码-> s = '0'.输入命令l来查看代码: 输入命令n可以单步执行代码: 任何时候都可以输入命令p 变量名来查看变量: (Pdb ...

  8. Another app is currently holding the yum lock; waiting for it to exit...另一个应用程序在占用yum lock,等待其退出。

    这种情况下通常是由于yum的时候意外退出造成的,虽然也给出提示当前占用进行的id,但是执行kill -9 3599 强制杀死进程后,情况依然没能改变. 备注:(-9是强制杀死) 主要原因是因为,yum ...

  9. zw版【转发·台湾nvp系列Delphi例程】HALCON GenGridRegion

    zw版[转发·台湾nvp系列Delphi例程]HALCON GenGridRegion unit Unit1;interfaceuses Windows, Messages, SysUtils, Va ...

  10. [ThinkPHP] 输出、模型的使用

    # # ThinkPHP 3.1.2 输出和模型使用 # 讲师:赵桐正 微博:http://weibo.com/zhaotongzheng   本节课大纲: 一.ThinkPHP 3 的输出      ...