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 ...
随机推荐
- DevSecOps 简介(一)
DevOps,或者说企业应用开发团队和系统运营团队的合作,已经成为一个时髦的 IT 话题.这一新的运营模式往往与敏捷式软件开发方法并举,同时还会利用云计算的可扩展性--这一切,都是为了使企业更加灵活, ...
- cf div2 238 c
C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- POJ 3384 Feng Shui(半平面交向内推进求最远点对)
题目链接 题意 : 两个圆能够覆盖的最大多边形面积的时候两个圆圆心的坐标是多少,两个圆必须在多边形内. 思路 : 向内推进r,然后求多边形最远的两个点就是能覆盖的最大面积. #include < ...
- 妙味课堂——HTML+CSS(第四课)(一)
这一课学的东西真是太多了,还不赶快记下来,留待以后慢慢回味! 首先我们回顾一下inline-block的特性: 使块元素在一行显示 使内嵌支持宽高 换行被解析了(问题) 不设置宽度的时候,宽度由内容撑 ...
- Log4net学习
转自:http://www.cnblogs.com/sirkevin/archive/2012/06/13/2548449.html Log4net简介根据日志类别保存到不同的文件,并按照日期生成不同 ...
- hdu 4223
暴力: Problem : ( Dynamic Programming? ) Judge Status : Accepted RunId : Language : C++ Author : yudun ...
- 安卓中bundle的使用
Bundle类用作携带数据,它类似于Map,用于存放key-value形式的值,相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,Bundle的内部实际上是使用了HashMa ...
- SendTextMessage如何打开记事本并显示指定内容
procedure TForm1.Button1Click(Sender: TObject); var hEdit: HWND; str: string; begin str := '准备要添加到记事 ...
- wordpress编辑器无法切换/输入
出现问题:“可视化”和“文本”无法切换,编辑区也无法输入文字 解决方法: 打开wp-config.php,在 require_once(ABSPATH . 'wp-settings.php'); 后面 ...
- sublime 支持PHP语法提示
下载插件phpcs>> https://github.com/benmatselby/sublime-phpcs 解压后修改文件夹名字为:Phpcs 把文件夹放到packages目录下 c ...