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人,人能 ...
随机推荐
- G面经prepare: set difference
给你A{1,2,3,4,4,5}, B{2,4},求A-B={1,3,4,5},很简单. visit 1 只用一个HashMap package TwoSets; import java.util.* ...
- Lintcode: Minimum Subarray
Given an array of integers, find the subarray with smallest sum. Return the sum of the subarray. Hav ...
- Lintcode: Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...
- 通达信:显示K线图日期
INFO_A:=STRCAT('INFO_A=', STRCAT(CON2STR(REF(MONTH, REF_BAR_A), 0), STRCAT('-', STRCAT(CON2STR(REF(D ...
- codeforces.com/problemset/problem/213/C
虽然一开始就觉得从右下角左上角直接dp2次是不行的,后面还是这么写了WA了 两次最大的并不一定是最大的,这个虽然一眼就能看出,第一次可能会影响第二次让第二次太小. 这是原因. 5 4 32 1 18 ...
- 当执行php脚本时用户关闭浏览器会发生什么?
2008年8月16日 1,152 views 发表评论 阅读评论 如果一段php脚本执行插入数据到mysql的操作. 一般情况下,由于php脚本在服务器上执行,此时用户虽然关闭了浏览器,但是服务器端的 ...
- 「thunar」给thunar增加搜索文件功能
1.安装catfish sudo apt-get install catfish 2.配置thunar,添加[自定义动作] 打开 Thunar 后,点击 Edit -> Configure cu ...
- jquery表单重置另一种方法
页面中按钮为<a>标签时,点击取消按钮,表单内容重置,需要给form表单id="form": <a class="demo_one1" onc ...
- FastJson之有道翻译
在AndroidMainifast.xml中加入相应的访问权限 <uses-permission android:name="android.permission.INTERNET&q ...
- 图像处理工具包ImagXpress教程:Accusoft不同组件间的图像数据传递
图像处理工具包ImagXpress的开发厂商Accusoft Pegasus旗下有多种图像处理相关的控件,但是这些图像处理控件之间的如何加传递图像数据呢?在ImagXpress 11版本之前,是需要将 ...