算法指南白书

分别求一次人和火到达各个点的最短时间

 #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int INF = ;
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][];//0表示人,1表示火走 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; // 必须是Joe可达的边界格子
if(!vis[r][c][] || d[r][c][] < d[r][c][]) ans = min(ans, d[r][c][] + ); // Joe必须先于火到达
} int main() {
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
vis[jr][jc][] = ;
d[jr][jc][] = ;
Q.push(Cell(jr, jc));
bfs(); // Fire
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 Fire! (bfs)的更多相关文章

  1. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  2. UVA 11624 Fire! bfs 难度:0

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. UVA 11624 Fire! BFS搜索

    题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...

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

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

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

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

  6. E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)

    E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...

  7. UVA 11624 - Fire! 图BFS

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

  8. UVa 11624 Fire!(BFS)

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

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

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

随机推荐

  1. 暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  2. 【POJ2094】【差分序列】Angry Teacher

    Description Mr. O'Cruel is teaching Math to ninth grade students. Students of course are very lazy, ...

  3. Python3 基础

    Hello world 在文本编辑器中,键入python执行代码,保存文件为hello.py print('hello, world') 注意print前面不要有任何空格.然后,选择一个目录,例如C: ...

  4. 新浪微博登录接口(PHP版)

    CI框架下 新浪微博登录接口完整版说明:本贴只适合CI框架.功能实现:登录接口跳转链接成功,获取用户信息(包括最重要的u_id)成功,将用户与本地平台连接起来,用户登录成功后信息的存储,本地数据库第三 ...

  5. dedecms插件开发教程

    这是一个很简单插件实例,通过此插件,你可以知道怎么样开发一个插件,它结构是怎么设置的,数据库,后台等.文件结构:enroll.php 文件在 plus文件下enroll.htm文件在templets/ ...

  6. 【javascript 函数基础知识】

    函数实际上是对象,每个函数都是 Function 类型的实例,而且都会与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定. [概念标签] ...

  7. [BUGFIX]__import_pywin32_system_module__

    import_pywin32_system_module 修复方法: 编辑 X:/Python27/Lib/site-packages/win32/lib/pywintypes.py 第114行 if ...

  8. 第一个CUDA程序

    开始学CUDA 先写一个简单的 #include<iostream>__global__ void add( int a, int b, int *c ) { *c = a + b;}in ...

  9. 动画讲解 Eclipse 常用快捷键

    Eclipse有强大的编辑功能, 工欲善其事,必先利其器, 掌握Eclipse快捷键,可以大大提高工作效率. 小坦克我花了一整天时间, 精选了一些常用的快捷键操作,并且精心录制了动画, 让你一看就会. ...

  10. HDU 4493 Tutor 水题的收获。。

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=4493 题意我都不好意思说,就是求12个数的平均数... 但是之所以发博客,显然有值得发的... 这个题最 ...