https://vjudge.net/problem/UVA-11624

题意:
有一个大火蔓延的迷宫,迷宫中有障碍格,而所有着火的格子都会往四周蔓延。求出到达边界格子时的最短时间。

思路:
复杂了一点的迷宫。

在bfs之前,我们首先需要计算出火势蔓延的情况,火势每次向四周蔓延一个格子,所以这也是一个最短路问题,也用一次bfs,计算出每个空白格子着火时的时间。这样,当我们第二次bfs去计算走出迷宫的时间时,对于每一个可走的格子,我们只需要判断在此时该格子是否着火,如果还未着火,则该格子是可以走的。

 #include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std; const int maxn = + ; int map[maxn][maxn];
int fire[maxn][maxn];
int vis[maxn][maxn]; int dx, dy;
int n, m; int sx[] = { , , , - };
int sy[] = { , -, , }; struct node
{
int x, y;
int t;
}; void bfs1()
{
memset(fire, -, sizeof(fire));
queue<node> Q;
for (int i = ; i < n;i++)
for (int j = ; j < m; j++)
{
if (map[i][j] == -)
{
node p;
p.x = i;
p.y = j;
p.t = ;
Q.push(p);
fire[i][j] = ;
}
}
while (!Q.empty())
{
node p = Q.front();
Q.pop();
for (int k = ; k < ; k++)
{
int x = p.x + sx[k];
int y = p.y + sy[k];
if (x < || x >= n || y < || y >= m) continue;
if (map[x][y] != ) continue;
if (fire[x][y] != -) continue;
fire[x][y] = p.t + ;
node u;
u.x = x;
u.y = y;
u.t = p.t + ;
Q.push(u);
}
}
} int bfs2()
{
memset(vis, , sizeof(vis));
queue<node> Q;
node p;
p.x = dx;
p.y = dy;
p.t = ;
Q.push(p);
vis[dx][dy] = ;
while (!Q.empty())
{
node p = Q.front();
Q.pop();
if (p.x == || p.x == n - || p.y == || p.y == m - ) return p.t;
for (int k = ; k < ; k++)
{
int x = p.x + sx[k];
int y = p.y + sy[k];
if (vis[x][y]) continue;
if (x < || x >= n || y < || y >= m) continue;
if (map[x][y] != ) continue;
if (fire[x][y]!=- && p.t + >= fire[x][y]) continue;
node u;
u.x = x;
u.y = y;
u.t = p.t + ;
Q.push(u);
vis[x][y] = ;
}
}
return -;
} int main()
{
ios::sync_with_stdio(false);
//freopen("D:\\txt.txt", "r", stdin);
int T;
char c;
cin >> T;
while (T--)
{
cin >> n >> m;
for (int i = ; i < n;i++)
for (int j = ; j < m; j++)
{
cin >> c;
if (c == '#') map[i][j] = ;
else if (c == 'F') map[i][j] = -;
else if (c == 'J')
{
map[i][j] = ;
dx = i;
dy = j;
}
else map[i][j] = ;
} bfs1();
int ans=bfs2();
if (ans == -) cout << "IMPOSSIBLE" << endl;
else cout << ans << endl;
}
}

UVa 11624 大火蔓延的迷宫的更多相关文章

  1. UVA11624大火蔓延的迷宫

    题意:     给1个n*m的网格,上面有的点能走,有的点不能走(墙),然后有的点是火源,火源和人一样,每次都是上下左右四个方向蔓延,速度一样是1,火也不可以从墙上跨过去,给你人的起点,终点是只要走到 ...

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

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

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

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

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

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

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

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

  6. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

  7. UVA 11624 Fire!(两次BFS+记录最小着火时间)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. UVa 11624,两次BFS

    题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...

  9. UVa 11624 (BFS) Fire!

    也是一个走迷宫的问题,不过又有了点变化. 这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的.问人是否能走出迷宫. 我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...

随机推荐

  1. Scala系统学习(二):Scala开发环境安装配置

    Scala可以安装在任何基于UNIX/Linux或基于Windows的系统上.在您的机器上开始安装Scala之前,必须在计算机上安装Java 1.8或更高版本. 下面请按照以下步骤安装Scala. 步 ...

  2. Py之pandas:dataframe学习【转载】

    转自:https://www.tutorialspoint.com/python_pandas/python_pandas_dataframe.htm 1.数据框4特性 列是不同类型的数据元素. 每列 ...

  3. [Leetcode] 336. Palindrome Pairs_Hard

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  4. 你应该知道的最好Webmail邮件客户端,

    1 . Kite Kite is an opensource replacement to Gmail. Kite is a webmail designed to look a lot like g ...

  5. Docker Quickstart Terminal: exit status 255 解决办法

    原文地址:https://www.jianshu.com/p/061f1ae69937 初识docker,还有先拿Windows 7 尝试下,官方提供了docker toolbox,下载后一键安装,桌 ...

  6. testng入门教程9 TestNG依赖测试

    有时候,你可能需要在一个特定的顺序调用方法在测试案例,或你想分享一些数据和方法之间的状态.TestNG支持这种依赖测试方法之间的显式依赖它支持声明. TestNG允许指定依赖,无论与否: 使用属性de ...

  7. Catch all the latest Jordan Release Dates

    In case y'all missed yesterday's news, Air Jordan 13 Olive 2018 officially unveiled their 2017 Holid ...

  8. HTML 显示/隐藏DIV的技巧(visibility与display的差别)

    参考链接:http://blog.csdn.net/szwangdf/article/details/1548807 div的visibility可以控制div的显示和隐藏,但是隐藏后页面显示空白: ...

  9. 如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?

    如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(2006-12-14 09:25:36) 与这个问题具有相同性质的其他描述还包括:如何 ...

  10. ModelSim使用$display查看变量值和输出信息

    打开ModelSim,新建工程->新建Verilog文件demo.v 输入文件内容 module demo(); reg[3:0] a,b; initial begin $display(&qu ...