算法指南白书

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

 #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. SQLServer:定时作业

    SQLServer:定时作业: 如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理-SQL Server代理-作业来实现 也快可以定时备 ...

  2. XML2_XML的节点和元素

    在JAVA语言中使用JAXP操作XML文件的时候,有两个接口,一个是Node,一个是Element,Element接口继承自Node接口. 在这一层次我们进一步理解XML中更具体的分类: 元素,属性, ...

  3. xe6+firedac连接sybase

    一.Win7 X64系统安装sybase odbc: 1.  下载对应包至c:\system_odbc(文件夹名自己取,在后面注册表内容需要用到): 2.  将值信息写入到注册表内: Windows ...

  4. 打开网页自动弹出qq客户端

    新建js后调用即可,打开网站自动弹出qq对话框,若qq为关闭状态则启动qq,之后弹出对话框. document.write("<iframe src='tencent://messag ...

  5. 实际中理解div布局和浮动

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 修改.htaccess实现子目录绑定示例分享

    <IfModule mod_rewrite.c>RewriteEngine On  RewriteBase /# 把 www.jb51.net改为你要绑定的域名.# 如果是域名:Rewri ...

  7. vlc

    源码下载地址:http://download.videolan.org/pub/videolan/vlc/ 编译依赖: sudo apt-get install liblua5.2-dev sudo ...

  8. ANDROID_MARS学习笔记_S02_014_GSON解析JSON串为对象

    package com.json2; import android.app.Activity; import android.os.Bundle; import android.view.View; ...

  9. 【Xamarin挖墙脚系列:mac 终端 常用命令+Mac OS X的快捷键+beamoff 】

    原文:[Xamarin挖墙脚系列:mac 终端 常用命令+Mac OS X的快捷键+beamoff ] 基本命令 1.列出文件 ls 参数 目录名        例: 看看驱动目录下有什么:ls /S ...

  10. Java 多维数组 按某列 排序

        public MetaCell[][] getByColumn(final int columnIndex, int decisionIndex) {//[注意]final咯          ...