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会向周围四个 ...
随机推荐
- SignalR实现服务器与客户端的实时通信
百度百科给它的定义 实现实时通信.什么是实时通信的Web呢?就是让客户端(Web页面)和服务器端可以互相通知消息及调用方法,当然这是实时操作的. WebSockets是HTML5提供的新的API,可以 ...
- 【Android】事件总线(解耦组件) EventBus 详解
当Android项目越来越庞大的时候,应用的各个部件之间的通信变得越来越复杂,例如:当某一条件发生时,应用中有几个部件对这个消息感兴趣,那么我们通常采用的就是观察者模式,使用观察者模式有一个弊病就是部 ...
- atitit.提升软件开发的效率and 质量的那些强大概念and方法总结
atitit.提升软件开发的效率and 质量的那些强大概念and方法总结 1. 主流编程中三个最糟糕的问题 1 1.1. 从理解问题后到实现的时间很长 1 1.2. 理解和维护代码 2 1.3. 学 ...
- Python模拟用户登录
# coding=utf8 import hashlib db = { 'michael':'e10adc3949ba59abbe56e057f20f883e', 'bob':'878ef96e861 ...
- 通过weburl 启动windows程序
1. 注册表修改 建立一个reg文件 执行导入 以RunLocal协议为例子 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\RunL ...
- Mysql:Error Code 1235,This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决 这次国庆节回来后的测试中,在一个Mysql表达式 ...
- linux下的依赖关系
1.一般来说依赖关系可以使得软件较小并且某个lib修复bug以后所有被依赖的软件都能得到好处. 依赖关系下,对于维护也有利有弊,第一,若某个被依赖的软件出现bug或者漏洞,这时候就只需要维护一个软件, ...
- RabbitMQ的安装使用
1.下载安装 Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装RabbitMQ之前要先安装Erlang. erlang:http://www.erlang.org/downloa ...
- Java 7 jstat – JVM Statistics Monitoring Tool【翻译】
原文地址:Java 7 jstat 本文内容 语法 参数 描述 虚拟机标识符 选项 一般选项 输出选项 示例 先发出来,然后慢慢翻译~ 语法 jstat [ generalOption | outpu ...
- Web开发人员常犯的10个错误
说到开发一个运行在现代网络中的网站:Web开发人员需要选择虚拟主机平台和底层数据存储,准备编写HTML.CSS和JavaScript用的工具,要有设计执行方式,以及一些可用的JavaScript库/框 ...