uva Fire 11624 bfs

Fire!

Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze.

Given Joe's location in the maze and which squares of themaze are on fire, you must determine whether Joe can exit the maze before thefire 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 fourdirections from each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the fire may enter asquare that is occupied by a wall.

Input Specification

The first line of input contains a single integer, thenumber of test cases to follow. The first line of each test case contains thetwo integers R and C, separated byspaces, with 1 <= R,C <= 1000. Thefollowing R lines of the test case each contain one row of themaze. Each of these lines contains exactly C characters, andeach of these characters is one of:

·  #, a wall

·  ., a passable square

·  J, Joe's initialposition in the maze, which is a passable square

·  F, a square that is onfire

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 cannotexit the maze before the fire reaches him, or an integer giving the earliesttime Joe can safely exit the maze, in minutes.

Output for SampleInput

3

IMPOSSIBLE

// UVa11624 Fire!
// Rujia Liu
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

const int INF = 1000000000;
const int maxr = 1000 + 5;
const int maxc = 1000 + 5;
int R, C;
char maze[maxr][maxc]; //存图

struct Cell {
  int r, c;
  Cell(int r, int c):r(r),c(c) {}
};  //构造函数

const int dr[] = {-1,1,0,0};
const int dc[] = {0,0,-1,1}; //四个方向
int d[maxr][maxc][2], vis[maxr][maxc][2]; //距离(时间)、访问标记

queue<Cell> Q;
void bfs(int kind) {    //两次bfs,kind为0对joe,为1时对fires进行bfs
  while(!Q.empty()) {
    Cell cell = Q.front(); Q.pop();  //出队
    int r = cell.r, c = cell.c;
    for(int dir = 0; dir < 4; dir++) {
      int nr = r + dr[dir], nc = c + dc[dir];
      if(nr >= 0 && nr < R && nc >= 0 && nc < C && maze[nr][nc] == '.' && !vis[nr][nc][kind]) {  //不出界并且没有访问过
        Q.push(Cell(nr, nc));  //入队
        vis[nr][nc][kind] = 1;
        d[nr][nc][kind] = d[r][c][kind] + 1; 
      }
    }
  }
}

int ans;
void check(int r, int c) {
  if(maze[r][c] != '.' || !vis[r][c][0]) return; // 必须是Joe可达的边界格子
  if(!vis[r][c][1] || d[r][c][0] < d[r][c][1]) ans = min(ans, d[r][c][0] + 1); // Joe必须先于火到达
} //Joe可达,fire应该也可达  

int main() {
  int T;
  scanf("%d", &T);
  while(T--) {
    scanf("%d%d", &R, &C);
    int jr, jc;
    vector<Cell> fires; //存fire
    for(int i = 0; i < R; i++) {   //存迷宫
      scanf("%s", maze[i]);
      for(int j = 0; j < C; j++)
        if(maze[i][j] == 'J') { jr = i; jc = j; maze[i][j] = '.'; } //记录Joe起点
        else if(maze[i][j] == 'F') { fires.push_back(Cell(i,j)); maze[i][j] = '.'; }  //记录fire位置
    }
    memset(vis, 0, sizeof(vis));

    // Joe
    vis[jr][jc][0] = 1; d[jr][jc][0] = 0;
    Q.push(Cell(jr, jc));
    bfs(0);

    // Fire
    for(int i = 0; i < fires.size(); i++) {
      vis[fires[i].r][fires[i].c][1] = 1;
      d[fires[i].r][fires[i].c][1] = 0;
      Q.push(fires[i]);   //超级源
    }
    bfs(1);

    // 计算答案
    ans = INF;
    for(int i = 0; i < R; i++) { check(i,0); check(i,C-1); }   //对迷宫的边界判断一遍
    for(int i = 0; i < C; i++) { check(0,i); check(R-1,i); }
    if(ans == INF) printf("IMPOSSIBLE\n"); else printf("%d\n", ans);
  }
  return 0;
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页