搜索(BFS)

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.

Sample
Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Output
Specification

For
each test case, output a single line containing IMPOSSIBLE if Joe cannot exit the maze before
the fire reaches him, or an integer giving the earliest time Joe can safely exit
the maze, in minutes.

Output
for Sample Input

3
IMPOSSIBLE

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

#include <iostream>

#include <cstdlib>

#include <cstring>

#include <cstdio>

#include <algorithm>

#include <string>

#include <queue>

#include <vector>

using
namespace std;

#define INF 1<<29

#define eps 1e-8

#define A system("pause")

#define rep(i,h,n) for(int i=(h);i<=(n);i++)

#define ms(a,b) memset((a),(b),sizeof(a))

#define lson l,mid,rt<<1

#define rson mid+1,r,rt<<1|1

const
int maxn=2000+5;

int
dx[]={0,1,-1,0};

int
dy[]={1,0,0,-1};

struct
dot

{

    int
x,y;

    bool
isfire;

    int
time;

};

int
vis[maxn][maxn];

char
map[maxn][maxn];

int
test,n,m;

inline
bool in(dot sgx)

{

     if(sgx.x>=0&&sgx.x<n&&sgx.y>=0&&sgx.y<m) return
true;

     return
false;

}

int
main()

{

    scanf("%d",&test);

    while(test--)

    {

         dot gx;

         scanf("%d%d",&n,&m);

         rep(i,0,n-1) scanf("%s",map[i]);

         queue<dot> q;

         while(!q.empty()) q.pop();

         ms(vis,0);

         rep(i,0,n-1) rep(j,0,m-1)

         {dot F;

              if(map[i][j]==‘J‘) gx.x=i,gx.y=j,gx.isfire=0,gx.time=0;

              else
if(map[i][j]==‘F‘)   vis[i][j]=1,F.x=i,F.y=j,F.isfire=1,F.time=0,q.push(F);

              else
if(map[i][j]==‘#‘)   vis[i][j]=1;

         }

         q.push(gx);

         int
ret=0;

         while(!q.empty())

         {

             dot next;

             gx=q.front(),q.pop();

             rep(i,0,3)

             {

                 next.x=gx.x+dx[i];

                 next.y=gx.y+dy[i];

                 next.isfire=gx.isfire;//BFS的时候注意"火势蔓延",相邻格子火势是相通的;

                 next.time=gx.time+1;

                 if(in(next))

                 {

                     if(!vis[next.x][next.y]) vis[next.x][next.y]=1,q.push(next);

                 }

                 else
if(next.isfire==0)

                 {

                     ret=next.time;//记录当前答案;

                     break;

                 }

             }

             if(ret>0)break;

         }

         if(!ret) std::puts("IMPOSSIBLE");

         else
printf("%d\n",ret);

    }

    //A;

    return
0;

}

时间: 2024-08-02 18:01:17

搜索(BFS)的相关文章

POJ 2329 (暴力+搜索bfs)

Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 Description Input is the matrix A of N by N non-negative integers. A distance between two elements Aij and Apq is defined as |i ? p| + |j ? q|. Your pro

hdu 4771 Stealing Harry Potter&#39;s Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: 目的:让你从 '@' 点出发,然后每个点只能走一次,求出最小的距离: 解题思路:先用 bfs 求解出任意两点之间的距离,用 ans[i][j],表示点 i 到点  j 的距离: 然后用 dfs 递归求出从起点经过所有点的距离中,比较出最小的: AC代码: 1 #include<iostream>

广度/宽度优先搜索(BFS)详解

广度/宽度优先搜索(BFS) [算法入门] 1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略.因为它的思想是从一个顶点V0开始,辐射状地优先遍历其周围较广的区域,故得名. 一般可以用它做什么呢?一个 广度/宽度优先搜索(BFS) 算法导论里边会给出不少严格的证明,我想尽量写得通俗一点,因此采用一些直观的讲法来伪装成证明,关键的point能够帮你get到就好. 2.图的概念 刚刚说的广度优先搜索是连通图的一种遍历策略,那就有必要将图先简单解释一下.

HDU1226 搜索 bfs xingxing在努力

这道题就是给你M个C进制的数, 然后让你求最小的数, 这个数是N的整数倍..搜索即可:剪枝条件:假设有两个数模N都为0那么我们就可以舍弃较大的那个数.为什么可以这样,我们可以假设这两个数是a, b a  = b (mod N)  => a*C + d = b*C + d (mod N), 然后注意取模的时候要用大数取摸的方式..坑点:N可能为0, 对于N==0的时候,我们应该特殊判断, 代码如下: #include <cstdio> #include <cstring> #i

广度优先搜索(BFS)

广度优先 Description: 阿狸被困在迷宫,snoopy要去救他,snoopy可以向上.下.左.右四个方向行走,每走一步(格)就要喝掉一瓶益力多.现在给它一个迷宫地图请问:snoopy最少需要多少瓶益力多才能走出迷宫? Input: 先输入一个数t,表示测试的数据个数, 下面输入的就是t个迷宫, 每个迷宫的输入都应包含以下数据, 输入迷宫的大小 n(n<=15),表示迷宫大小为n*n. 再输入迷宫, 用大写字母“S”表示snoopy的位置, 用小写字母“E”表示阿狸被困的位置, 用“.”

【算法导论】--C++实现广度优先搜索bfs

一.题目 根据上次随机生成的100个顶点的无向图和有向图,对其进行广度优先搜索. 二.理解广度优先搜索 广度优先搜索可以将其想象成水滴落入水面溅起了的一圈一圈的涟漪,是由一个起始点开始一圈一圈进行扩散搜索的. [课上老师是这样说的,大家想象一下,发现其实非常形象] 广度优先搜索总是从一个起始点出发,首先扩散这个点周围所有的邻居,然后邻居在去扩散邻居的邻居(*^-^*)...然后一直到最后将整张图都扩散完. 三.代码实现 对于第一次随机生成100个顶点的图进行了细节的修改,将每个顶点的类型改为了自

HDU 3220 IDA*搜索 || BFS

给出一个16个点所构成的图形,分别由0,1组成,每次操作可以任选其中相连的两个点(必须一个为0,一个为1),进行0,1,交换 问3步内是否可以把图形变成:0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1状态 若不行,输出more 状压存储图形状态.ida*搜索  或者 BFS都行 IDA*搜索 #include "stdio.h" #include "string.h" const int b[]={0,1,2,4,8,16,32,64,128,256

深度优先搜索DFS和广度优先搜索BFS

DFS简介 深度优先搜索,从起点开始按照某个原则一直往深处走,直到找到解,或者走不下去,走不下去则回溯到前一节点选择另一条路径走,直到找到解为止. BFS简介 广度优先搜索,从起点开始先搜索其相邻的节点,由此向外不断扩散,直到找到解为止. 举例解释 从1开始去寻找5 DFS: 原则:优先选择左手边 过程:1-2-3-4-6-4-5 BFS: 队列情况:1 2.5     5.3 5出来则找到 遍历图中所有点 DFS: 原则:优先选择左手边 过程:1-2-3-4-6-4-5 BFS: 队列情况:1

算法与数据结构基础 - 广度优先搜索(BFS)

BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数据结构基础 - 队列(Queue) 最直观的BFS应用是图和树的遍历,其中图常用邻接表或矩阵表示,例如 LeetCode题目 690. Employee Importance: // LeetCode 690. Employee Importance/* class Employee { publi

广度优先搜索bfs C++实现

#include<iostream>  #include<vector>  #include<map>  #include<queue>  #include<set>  using namespace std;    vector<int> bfs(map<int, vector<int> > link, int top)  {      queue<int> qe;  //队列用来记录节点的遍历顺