UVa 11624 (BFS) Fire!
也是一个走迷宫的问题,不过又有了点变化。
这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的。问人是否能走出迷宫。
我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间。
第二遍便是走迷宫,只有当这个格子不是墙,而且当前时间在这个格子着火之前才能拓展。当然,并不是所有的空格都一定会着火。
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; struct Node
{
int x, y, t;
Node(int x=, int y=, int t=):x(x), y(y), t(t) {}
}; Node st; const int maxn = + ;
int row, col;
char maze[maxn][maxn];
int time[maxn][maxn];
bool vis[maxn][maxn]; int dx[] = { , , , - };
int dy[] = { , , -, }; inline bool in(int x, int y)
{ return x >= && x < row && y >= && y < col; } inline bool border(int x, int y)
{ return x == || x == row- || y == || y == col-; } void init()
{
memset(time, -, sizeof(time));
queue<Node> Q;
for(int i = ; i < row; i++)
for(int j = ; j < col; j++)
{
if(maze[i][j] == 'F') { Q.push(Node(i, j, )); time[i][j] = ; }
if(maze[i][j] == 'J') { st.x = i; st.y = j; st.t = ; }
}
while(!Q.empty())
{
Node now = Q.front(); Q.pop();
for(int i = ; i < ; i++)
{
int x = now.x + dx[i];
int y = now.y + dy[i];
int t = now.t + ;
if(in(x, y) && time[x][y] < && maze[x][y] != '#')
{
Q.push(Node(x, y, now.t + ));
time[x][y] = t;
}
}
}
} int BFS()
{
memset(vis, false, sizeof(vis));
queue<Node> Q;
Q.push(st);
vis[st.x][st.y] = true;
while(!Q.empty())
{
Node now = Q.front(); Q.pop();
if(border(now.x, now.y)) return now.t + ;
for(int i = ; i < ; i++)
{
int x = now.x + dx[i];
int y = now.y + dy[i];
int t = now.t + ;
if(in(x, y) && !vis[x][y] && maze[x][y] != '#')
{
if(time[x][y] >= && time[x][y] <= t) continue;
vis[x][y] = true;
Q.push(Node(x, y, t));
}
}
}
return ;
} int main()
{
//freopen("in.txt", "r", stdin); int T; scanf("%d", &T);
while(T--)
{
scanf("%d%d", &row, &col);
for(int i = ; i < row; i++) scanf("%s", maze[i]);
init();
int ans = BFS();
if(ans) printf("%d\n", ans);
else puts("IMPOSSIBLE");
} return ;
}
代码君
UVa 11624 (BFS) Fire!的更多相关文章
- UVA - 11624 J - Fire! (BFS)
题目传送门 J - Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ...
- uva 11624(bfs)
11624 - Fire! Time limit: 1.000 seconds Joe works in a maze. Unfortunately, portions of the maze hav ...
- UVA 11624 BFS的妙用
题意: 迷宫里起火了,有若干个障碍物,有多个起火点,起火点每经过一个时间间隔就向它的上下左右相邻的格子扩散. 有个倒霉的人好像叫做“Joe”,他要逃出来,他每次可以向上下左右任意移动一格,但是即要避开 ...
- 【UVA - 11624】Fire!
-->Fire! 直接上中文 Descriptions: 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块 ...
- (UVA 11624)Fire!
题目链接 http://vjudge.net/contest/121377#problem/J Joe works in a maze. Unfortunately, portions of the ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
随机推荐
- __dict__和__slots__
__dict__: __slots__:
- Loadrunner监控Centos
一.安装必要包 yum istall gcc gcc-c++ rpcbind -y 二.下载安装必要软件rstatd 下载并安装rstatd,下载地址:http://sourceforge.net/p ...
- Gdata XML解析配置和简单使用
导入libxml2,使用第三方AFNetworking网络请求,第三方XML解析GData GData需要的配置 Build Settings 里搜索,添加如下
- 利用正则表达式解析URL
-(NSString *) jiexi:(NSString *)CS webaddress:(NSString *)webaddress { NSError *error; NSS ...
- POJ 2533 Longest Ordered Subsequence
题目描述:LIS(Longest Increasing Subsequence)模板题 分析:O(n^2)的方法 状态表示:d[i]表示以i结尾的最长上升子序列长度 转移方程:d[i]=max{ 1, ...
- MetInfo安装
安装MetInfo企业网站管理系统需要经历三个步骤:安装准备.上传文件.安装系统. 第一步:安装前的准备 环境要求:需要支持PHP并提供Mysql数据库的空间(虚拟主机),详细要求: 下载MetInf ...
- ***PHP preg_match正则表达式的使用
第一,让我们看看两个特别的字符:‘^’和‘$’他们是分别用来匹配字符串的开始和结束,以下分别举例说明 : "^The": 匹配以 "The"开头的字符串; &q ...
- 深入理解javascript:揭秘命名函数表达式
这是一篇转自汤姆大叔的文章:http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html 前言 网上还没用发现有人对命名函数表达式进去重复深 ...
- 开始使用Mac OS X——写给Mac新人
本文转自博客园:http://www.cnblogs.com/chijianqiang/archive/2011/08/03/2126593.html 写这篇文档的原因有两个,一.身边使用Mac的朋友 ...
- [RM HA3] Zookeeper在RM HA的应用
[RM HA3] Zookeeper在RM HA的应用 RM HA(ResourceManager HighAvailability)中使用Zookeeper的地方在ZKRMStateStore和Z ...