码迷,mamicode.com
首页 > 其他好文 > 详细

搜索(BFS)

时间:2014-05-07 13:28:07      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   ext   color   

Problem B: Fire!

bubuko.com,布布扣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.

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;
}

 

搜索(BFS),布布扣,bubuko.com

搜索(BFS)

标签:style   blog   class   code   ext   color   

原文地址:http://www.cnblogs.com/scottding/p/3712838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!