算法指南白书

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

 #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. 滑动条 Trackbar[OpenCV 笔记9]

    OpenCV中没有实现按钮的功能,我们可以利用滑动条来实现按钮功能. , ); trackbarname 轨迹条的名字. winname 窗口的名字,轨迹条会依附在这个窗口上. value 一个指向整 ...

  2. struts2初印象

    第一次写这么正式的文章,如果写的不好的地方,请指出. 今天玩了一下struts2,不过貌似是我被他玩了.简要笔记如下: 一.配置struts2(在eclipse Helios版本下) (1)先创建一个 ...

  3. (转)Libevent(4)— Bufferevent

    转自:http://name5566.com/4215.html 参考文献列表:http://www.wangafu.net/~nickm/libevent-book/ 此文编写的时候,使用到的 Li ...

  4. 排序算法——QuickSort、MergeSort、HeapSort(C++实现)

    快速排序QuickSort template <class Item> void quickSort (Item a[], int l, int r) { if (r<=l) ret ...

  5. 深入了解overflow

    1.如果overflow-x与overflow-y值不同   其中一个赋值为visiable,另一个赋值scroll/auto/hidden,那么visiable会重置为auto 2.overflow ...

  6. 移动端REM布局方案

    引用http://www.w3cplus.com/mobile/lib-flexible-for-html5-layout.html的方案 下载地址https://github.com/hupan50 ...

  7. Oracle课堂实验一“表的使用”代码。

    --创建本地管理表空间CustomerTBSCREATE TABLESPACE CustomerTBS         DATAFILE 'd:\Oracle11\product\11.2.0\ora ...

  8. 由 OR 引起的死循环

    在客商迁移测试时,程序一旦开始执行就不能自动停止.只能通过手动中断应用服务器的进程来停止.检查迁移的一个表,这个表迁移前没有数据,迁移最多会插入3w条左右数据,但是迁移过程执行2个多小时候再看,已经有 ...

  9. POJ 1716 Integer Intervals 差分约束

    题目:http://poj.org/problem?id=1716 #include <stdio.h> #include <string.h> #include <ve ...

  10. PBS

    赞同,已经试验成功.后来查手册: $man qdel-p 的功能是强制净化队列.这个 "p" 可能是  "purge" 的缩略形式 qsub,qdel,qmgr ...