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. ios 手动添加mapview

    1,首先选中Build Phases ,在Link Binary With Libraries 下添加MapKit.framework框架 2,在头文件(.h文件)处添加:#import <Ma ...

  2. C语言实现二叉树-01版

    故事是这样开始的,项目经理有一天终于还是拍拍我肩膀说: 无论你的链表写得多么的好,无论是多么的灵活,我也得费老半天才查找到想要的数据: 这让我的工作非常苦恼,听说有一种叫做二叉树的数据结构,你看能不能 ...

  3. atititt.java定时任务框架选型Spring Quartz 注解总结

    atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz  (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...

  4. 已知2个一维数组:a[]={3,4,5,6,7},b[]={1,2,3,4,5,6,7};把数组a与数组b 对应的元素乘积再赋值给数组b,如:b[2]=a[2]*b[2];最后输出数组b的元素。

    package hanqi; import java.util.Scanner; public class Test7 { public static void main(String[] args) ...

  5. Mybatis多参传递的四种解决方案

    Mybatis多参传递的四种解决方案 代码异常:org.apache.ibatis.binding.BindingException: Parameter 'param' not found. 长时间 ...

  6. iOS7隐藏状态栏 statusBar

    转:http://blog.csdn.net/dqjyong/article/details/17896145 评:通过这点变化,可以看出苹果倾向于使用delegate取代全局变量. IOS7中,不仅 ...

  7. libuv在cocos2d-x中的使用

    libuv经过Node.js的实践和应用,已经证明非常之成熟,本来之前项目用的是这个:clsocket https://github.com/DFHack/clsocket  当初选它的主要原因是它支 ...

  8. 水星Mercury路由器端口映射设置图文方法

    在一些内网的环境里,你可能需要把自己的内网的WEB服务器或者其他应用服务器设置成通过互联网可以访问,但是在内网我们是通过路由器共享上网的,外网无法访问到我们的内部服务器.那么这就需要我们通过" ...

  9. mysql-5.7.15-winx64免安装版配置

    1.拷到硬盘根目录下; 2.在 bin 平行目录下新建  data 文件夹: 3. 修改  my-default.ini 文件,添加 basedir = C:\mysql-5.7.15-winx64d ...

  10. JS 获取自定义标签

    <abc-aaa xwe='sdf'>AAAAAAAAAAAAAAAAAAAAAA</abc-aaa> alert($("abc-aaa").attr(&q ...