UVa 11624,两次BFS
题目链接: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的更多相关文章
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- 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
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- (简单) 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】
题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...
- uva 11624 Fire! 【 BFS 】
按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 #include<cstdio> #include<cstring> #include&l ...
- UVA 11624 Fire!(两次BFS+记录最小着火时间)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Fire! UVA - 11624 (两步bfs)
题目链接 题意 人要从迷宫走出去,火会向四个方向同时扩散 分析 两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能 ...
随机推荐
- Swift语言实战晋级-第9章 游戏实战-跑酷熊猫-1
学习目标 一.进一步学习Swift的游戏制作 二.掌握SKNode,SKSpriteNode的运用 三.了解SpriteKit的物理系统 四.掌握动作(SKAction)的运用 在这一章,我们要通过制 ...
- 强连通+二分匹配(hdu4685 Prince and Princess)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- 在 msbuild 生成时,每次都生成很多css,js,img 文件,如果不想每次编译都有这些需要这样配置
在 msbuild 不想生成一大堆文件,可以在 Web 项目的csproj 文件里,注释掉这一串. <!--<Import Project="$(VSToolsPath)\Web ...
- .net 网站预编译命令
aspnet_compiler -v /Aspnet -p "C:\inetpub\wwwroot\a" C:\inetpub\wwwroot\a2 /Aspnet iis ...
- SQL2005中的事务与锁定(二)- 转载
------------------------------------------------------------------------ -- Author : HappyFlyStone ...
- android 项目学习随笔七(ViewPagerIndicator与ViewPager)
1.ViewPagerIndicator https://github.com/JakeWharton/ViewPagerIndicator package com.viewpagerindicato ...
- 好用的 diskpart 命令,操作磁盘分区,并创建 EFI 引导分区
进入 win8 pe, 或在安装 win8 选择安装磁盘分区时按 shift+F10,都是可能用到 diskpart 去操作的时候. 进入cmd后, diskpart 进入 list disk---- ...
- gerrit
https://en.wikibooks.org/wiki/Git/Gerrit_Code_Review http://openwares.net/linux/gerrit2_setup.html 不 ...
- 160920、springmvc上传图片不生成临时文件
springMVC上传图片时候小于10k不会再临时目录里面生成临时文件,需要增加一个配置 <property name="maxInMemorySize" value=&qu ...
- Backup: Numbers in Perl6
Perl6 is a new language, not a improved version of Perl5. Perl6 inherits many good features from man ...