uva 11624(bfs)
11624 - Fire!
Time limit: 1.000 seconds
Joe works in a maze. Unfortunately, portions of the maze have
caught on re, and the owner of the maze neglected to create a re
escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze
are on re, you must determine whether Joe can exit the maze before
the re reaches him, and how fast he can do it.
Joe and the re each move one square per minute, vertically or
horizontally (not diagonally). The re spreads all four directions
from each square that is on re. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the re
may enter a square that is occupied by a wall.
Input
The rst line of input contains a single integer, the number of test
cases to follow. The rst line of each test case contains the two
integers R and C, separated by spaces, with 1 R; C 1000. The
following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
C characters, and each of these characters is one of:
#, a wall
., a passable square
J, Joe's initial position in the maze, which is a passable square
F, a square that is on re
There will be exactly one J in each test case.
Output
For each test case, output a single line containing `IMPOSSIBLE' if Joe cannot exit the maze before the
re reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output
3
IMPOSSIBLE
简单bfs
开始提议理解错了,wrong了好几次,才知道原来可以有多个火源地。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 1005
char s[MAXN];
int ma[MAXN][MAXN];
bool vis[MAXN][MAXN];
int d[][] = {{, }, {, }, {, -}, {-, }};
bool flag;
struct P
{
int x, y, s;
P() : x(), y(), s() {}
P(int x_, int y_, int s_) : x(x_), y(y_), s(s_) {}
};
P J, F;
queue<P> q, Q;
int r, c; int Judge(int x, int y)
{
if(x > && y > && x <= r && y <= c) return ;
return ;
} void init()
{
while(!q.empty()) q.pop();
while(!Q.empty()) Q.pop();
_cle(ma, );
_cle(vis, false);
flag = false;
} int bfs()
{
vis[J.x][J.y] = ;
q.push(J);
int last = ;
while(!q.empty())
{
P t = q.front();
q.pop();
if(t.x == || t.y == || t.x == r || t.y == c)
return t.s + ;
if(flag && last != t.s + )
{
int siz = Q.size();
while(siz--)
{
P p = Q.front();
Q.pop();
repu(i, , )
{
int dx = p.x + d[i][];
int dy = p.y + d[i][];
if(Judge(dx, dy) && ma[dx][dy] == )
{
ma[dx][dy] = ;
Q.push(P(dx, dy, t.s));
}
}
}
last = t.s + ;
}
repu(i, , )
{
int dx = t.x + d[i][];
int dy = t.y + d[i][];
if(Judge(dx, dy) && !vis[dx][dy] && ma[dx][dy] == )
{
vis[dx][dy] = ;
q.push(P(dx, dy, t.s + ));
}
}
}
return -;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
init();
scanf("%d%d", &r, &c);
repu(i, , r)
{
scanf("%s", s);
repu(j, , c) if(s[j] == '.') ma[i + ][j + ] = ;
else if(s[j] == 'F')
{
Q.push(P(i + , j + , ));
flag = true;
ma[i + ][j + ] = ;
}
else if(s[j] == 'J')
{
J.x = i + , J.y = j + , J.s = ;
ma[i + ][j + ] = ;
}
}
int re = bfs();
if(re == -) printf("IMPOSSIBLE");
else printf("%d", re);
puts("");
}
return ;
}
uva 11624(bfs)的更多相关文章
- UVA 11624 BFS的妙用
题意: 迷宫里起火了,有若干个障碍物,有多个起火点,起火点每经过一个时间间隔就向它的上下左右相邻的格子扩散. 有个倒霉的人好像叫做“Joe”,他要逃出来,他每次可以向上下左右任意移动一格,但是即要避开 ...
- UVa 11624 (BFS) Fire!
也是一个走迷宫的问题,不过又有了点变化. 这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的.问人是否能走出迷宫. 我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...
- UVA 11624 Fire! bfs 难度:0
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
随机推荐
- oracle命令识记
连接数据库 sqlplus /nolog; conn / as sysdba; set ORACLE_SID=实例名; 查看表结构命令 select table_name from user_tabl ...
- CUBRID学习笔记 25 数据类型2
---恢复内容开始--- 6枚举类型 语法 <enum_type> : ENUM '(' <char_string_literal_list> ')' <char_str ...
- So easy Webservice 8.spring整合CXF 发布WS
1.添加jar包(cxf的jar包中包含了spring的jar包),添加spring配置文件 2.web.xml中配置CXFServlet,过滤WS服务的地址 <!-- 配置CXFServlet ...
- 3.29考试(HNOI难度)
一. 城镇 [ town ] Memory Limit: 128 MB Time Limit : 1s Description 在 farmer land 上,有 N 个 farmer to ...
- java或者jsp中修复会话标识未更新漏洞
AppScan会扫描“登录行为”前后的Cookie,其中会对其中的JSESSIONOID(或者别的cookie id依应用而定)进行记录.在登录行为发生后,如果cookie中这个值没有发生变化,则判定 ...
- Linux_常用命令_04_挂载
1. mount [-t vfstype] [-o options] device dir ZC: -o 后面跟多个option的话,用逗号隔开.(例如:"mount -o rw,remou ...
- web设计经验<七>13步打造优雅的WEB字体
今天,大多数浏览器已经默认支持Web字体,日趋增多的字体特性被嵌入最新版HTML和CSS标准中,Web字体即将迎来一个趋于复杂的崭新时代.下面是一些基本的关于字体的规则,特别适用于Web字体. 原文地 ...
- Javascript设计模式之匿名函数与闭包
匿名函数 (function () { var foo = 10; var bar = 2; console.log(foo*bar); })(); // 20 带参数的匿名函数 (function ...
- Hadoop与Spark比较
先看这篇文章:http://www.huochai.mobi/p/d/3967708/?share_tid=86bc0ba46c64&fmid=0 直接比较Hadoop和Spark有难度,因为 ...
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处http://blog.csdn.net/xiaanming/article/details ...