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 ...
随机推荐
- 强力重置ASP.NET membership加密后的密码![转]
公司网站的用户管理采用的是ASP.NET内置的membership管理,在web.config文件中的密码格式配置是加密了的,passwordFormat="Hashed",这样在 ...
- RMQ(st)
int dp[1111][12]; int a[1111]; int n; void RMQ_init() { for(int i=1;i<=n;i++) { d ...
- UnityException: Texture is not readable
原地址:http://blog.csdn.net/emoonight/article/details/18002913 fore you can save or load a Texture, you ...
- Ubuntu下开启ssh服务
网上有很多介绍在Ubuntu下开启SSH服务的文章,但大多数介绍的方法测试后都不太理想,均不能实现远程登录到Ubuntu上,最后分析原因是都没有真正开启ssh-server服务.最终成功的方法如下: ...
- 程序员必须知道的git托管平台
http://www.open-open.com/lib/view/open1420704561390.html
- Android 源码 判断网络数据类型
private final void updateDataNetType(int slotId) { int tempDataNetType; NetworkType tempDataNetType3 ...
- MY_Log,无缝替换原生Log,支持日志输出到文件、FirePHP
自己扩展了一个MY_Log, 用法类似于log4j,目前支持将日志输出到文件.FirePHP.如果你需要将日志输出到其他地方,比如邮件.数据库等,可以很方便地进行扩展. 用法很简单,大家一看就知道.1 ...
- 全注解的SSH框架
基于struts2.23 + spring2.5.6 + hibernate3.6.4 + hibernate-generic-dao1.0(除了spring,我整合的都是最新的GA包,hiberna ...
- 关于在linux下清屏的几种技巧
在windows的DOS操作界面里面,清屏的命令是cls,那么在linux 里面的清屏命令是什么呢?下面笔者分享几种在linux下用过的清屏方法. 1.clear命令.这个命令将会刷新屏幕,本质上只是 ...
- lintcode:带环链表
带环链表 给定一个链表,判断它是否有环. 解题 定义两个指针p1 p2 p1每次向前走一步 p2每次向前走两步 当p2能赶上p1的时候说明有环 /** * Definition for ListNod ...