Fire!(BFS)
Description
Problem B: Fire!
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.
Input Specification
The first line of input contains a single integer, the number of test cases to follow. The first 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 fire
There will be exactly one J in each test case.
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring> using namespace std; struct Pos
{
int y, x, step;
Pos(int a, int b, int c) {y = a; x = b; step = c;}
};
int r, c, fi, fj, ji, jj;
char m[][];
queue<Pos> que;
int fx[], fy[], top, tail; const int d[][] = {{, }, {, -}, {, }, {-, }}; bool Judge(int i, int j)
{
return i > - && j > - && i < r && j < c && m[i][j] == '.';
} bool Judge2(int i, int j)
{
return i > - && j > - && i < r && j < c && m[i][j] != '#' && m[i][j] != 'F';
} bool isExit(int i, int j)
{
return i == || j == || i == r - || j == c - ;
} void fire_spread()
{
int y, x, yy, xx, end = tail;
for(; top < end; top++){
y = fy[top];
x = fx[top];
for(int i = ; i < ; i++){
yy = y + d[i][];
xx = x + d[i][];
if(Judge2(yy, xx)){
m[yy][xx] = 'F';
fx[tail] = xx;
fy[tail] = yy;
tail++;
}
}
}
} int main()
{
int t;
scanf("%d", &t);
while(t--){
bool ok = false;
top = tail = ;
while(!que.empty()) que.pop();
scanf("%d %d", &r, &c);
for(int i = ; i < r; i++){
scanf("%s", m[i]);
for(int j = ; m[i][j] != '\0'; j++){
if(m[i][j] == 'J') que.push(Pos(i, j, ));
else if(m[i][j] == 'F') fy[tail] = i, fx[tail] = j, tail++;
}
}
int pre = ;
fire_spread();
while(!que.empty()){
Pos cur = que.front();
que.pop();
if(isExit(cur.y, cur.x)){
pre = cur.step + ;
ok = true;
break;
}
if(cur.step > pre) {
fire_spread();
pre++;
}
for(int i = ; i < ; i++){
int ii = cur.y + d[i][];
int jj = cur.x + d[i][];
if(Judge(ii, jj)) {
m[ii][jj] = 'P';
que.push(Pos(ii, jj, cur.step + ));
}
}
}
if(!ok) puts("IMPOSSIBLE");
else printf("%d\n", pre);
}
return ;
}
Fire!(BFS)的更多相关文章
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA11624 Fire! —— BFS
题目链接:https://vjudge.net/problem/UVA-11624 题解: 坑点:“portions of the maze havecaught on fire”, 表明了起火点不唯 ...
- UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...
- UVA 11624 Fire! bfs 难度:0
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- foj 2150 Fire Game(bfs暴力)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...
- fzu 2150 Fire Game 【身手BFS】
称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...
随机推荐
- Objective-C与C style语言的简单类比
1. 关于Objc中函数调用类比 [_lblHelloWorld setHidden:![_lblHelloWorld isHidden]]; 类比为: _lblHelloWorld.setHidde ...
- 如何处理Win7连接vpn时报错789的问题
[转]VPN错误提示: vpn连接出错789:L2TP连接尝试失败,因为安全层在初始化与远程计算机的协商时遇 (2014-08-11 15:09:10)转载▼标签: it xp连接VPN错误提示: v ...
- SqlServer2008R2用Windows身份登录18456错误解决
// 重装系统后发现SqlServer2008R2使用Windows身份验证不能进行连接,如下图: 以前经常会碰到SqlServer 身份验证连接失败,Windows身份验证的连接失败还是第一次,试了 ...
- 取代奶瓶Minidwep-gtk破解WPA 全攻略
取代奶瓶Minidwep-gtk 破 WPA 全攻略 目录 1. CDlinux 下使用 minidwepgtk 获取握手包并使用自带的字典破解 2. 自带的字典破解不出密码时使用 U 盘外挂字典继 ...
- C# 传入引用类型的参数 返回值是否发生变化
前一段时间做项目是,一YY说如果一个方法的参数是引用类型,那么在这个方法里面所做的所有的修改再方法调用后应该有体现.事实是这样的吗? 先看code 和运行结果: 运行结果 方法SetPersonInf ...
- Android SDK Tools Platform-tools Build-tools
(1)Android SDK (Android SDK主安装包,包含SDK Manager.AVD Manager.工具包tools,释放后的根文件夹为android-sdk-windows): re ...
- nginx 重写 rewrite 基础及实例
nginx rewrite 正则表达式匹配 大小写匹配 ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配 -f和!-f用来判断是否 ...
- Win C盘扩容
最近C盘真的空间越来越小了,记下简单的几个操作步骤以便以后用到. 参考文档: http://jingyan.baidu.com/article/90808022a6c6b7fd91c80fc8.htm ...
- 最新的JavaScript核心语言标准——ES6,彻底改变你编写JS代码的方式!【转载+整理】
原文地址 本文内容 ECMAScript 发生了什么变化? 新标准 版本号6 兑现承诺 迭代器和for-of循环 生成器 Generators 模板字符串 不定参数和默认参数 解构 Destructu ...
- Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...