Fire!

Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

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 <= RC <= 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)的更多相关文章

  1. UVA 11624 Fire! (bfs)

    算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...

  2. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  3. UVA11624 Fire! —— BFS

    题目链接:https://vjudge.net/problem/UVA-11624 题解: 坑点:“portions of the maze havecaught on fire”, 表明了起火点不唯 ...

  4. UVA 11624 Fire! BFS搜索

    题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...

  5. UVA 11624 Fire! bfs 难度:0

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

    FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  7. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  8. foj 2150 Fire Game(bfs暴力)

         Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...

  9. fzu 2150 Fire Game 【身手BFS】

    称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...

随机推荐

  1. 常用linux命令索引

    每天一个linux命令(61):wget命令 每天一个linux命令(60):scp命令 每天一个linux命令(59):rcp命令 每天一个linux命令(58):telnet命令 每天一个linu ...

  2. SHINY-SERVER R(sparkR)语言web解决方案 架设shiny服务器

    1. shiny server简介 shiny-server是一种可用把R 语言以web形式展示的服务,其实RStudio公司自己构建了R Shiny Application运行的平台(http:// ...

  3. 【转】Oracle 执行动态语句

    1.静态SQLSQL与动态SQL Oracle编译PL/SQL程序块分为两个种:其一为前期联编(early binding),即SQL语句在程序编译期间就已经确定,大多数的编译情况属于这种类型:另外一 ...

  4. FIR.im Weekly - 技术是练出来的

    本期 Weekly 主要精选了上周一些不错的 GitHub 资源.开发工具和技术实践教程类文章分享给大家. JSPatch – 动态更新 iOS APP JSPatch 是 @Bang 最近业余做的小 ...

  5. spring 配置定时任务

    spring的定时任务配置分为三个步骤:1.定义任务2.任务执行策略配置3.启动任务1.定义任务 <!--要定时执行的方法--> <bean id="testTaskJob ...

  6. IOS开发之控件篇UITabBarControllor第一章 - 介绍

    UITabBarControllor的基本样子 官方有个图介绍这个TabBar的结构,我们先来看看这个结构图 --------------------------------------------- ...

  7. Struts2学习笔记-基本结构

    一个普通的的web应用文件结构如下: 1. 最上层是应用名,区分大小写,在浏览器输入应用名的时候,必须与应用名的大小一样,例如:localhost:8080/HelloWorld 2. 在应用下,放有 ...

  8. MongoDB学习笔记——索引管理

    索引 索引能够提升查询的效率.没有索引,MongoDB必须扫描集合中的所有文档,才能找到匹配查询语句的文档. 索引是一种特殊的数据结构,将一小块数据集保存为容易遍历的形式.索引能够存储某种特殊字段或字 ...

  9. Android MultiDex兼容包怎么使用?

    在Android系统中安装应用的时候,需要对Dex进行优化,但由于其处理工具DexOpt的限制,导致其id的数目不能够超过65536个.而MultiDex兼容包的出现,就很好的解决了这个问题,它可以配 ...

  10. EnumHelper枚举常用操作类

    在项目中需要把枚举填充到下拉框中,所以使用统一的方法实现,测试代码如下: namespace CutPictureTest.Comm { public class EnumHelper { publi ...