24h購物| | PChome| 登入
2012-12-26 20:31:37| 人氣789| 回應0 | 上一篇 | 下一篇

[UVA][bfs] 11624 - Fire!

推薦 0 收藏 0 轉貼0 訂閱站台

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 <= 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

我忘記也有火到達不了的地方,但是為了節省空間,我忘了我的 F[i][j] 是 0,導致判斷錯誤。

#include <stdio.h>
#include <queue>
using namespace std;
char g[1010][1010];
int F[1010][1010], J[1010][1010];
int n, m, dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
struct pt {
int x, y;
};
pt pf, pj;
queue<pt> Q;
void sol() {
int i, j;
while(!Q.empty()) Q.pop();
pt tn, tx;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
if(g[i][j] == 'F') {
pf.x = i, pf.y = j;
Q.push(pf);
F[pf.x][pf.y] = 1;
}
while(!Q.empty()) {
tn = Q.front();
Q.pop();
for(i = 0; i < 4; i++) {
tx.x = tn.x+dir[i][0];
tx.y = tn.y+dir[i][1];
if(tx.x < 0 || tx.x >= n || tx.y < 0 || tx.y >= m)
continue;
if(g[tx.x][tx.y] == '#')
continue;
if(F[tx.x][tx.y] == 0) {
F[tx.x][tx.y] = F[tn.x][tn.y]+1;
Q.push(tx);
}
}
}
Q.push(pj), J[pj.x][pj.y] = 1;
while(!Q.empty()) {
tn = Q.front();
Q.pop();
if(tn.x == 0 || tn.y == 0 || tn.x == n-1 || tn.y == m-1) {
printf("%d\n", J[tn.x][tn.y]);
return;
}
for(i = 0; i < 4; i++) {
tx.x = tn.x+dir[i][0];
tx.y = tn.y+dir[i][1];
if(tx.x < 0 || tx.x >= n || tx.y < 0 || tx.y >= m)
continue;
if(g[tx.x][tx.y] == '#')
continue;
if(J[tx.x][tx.y] == 0 && (J[tn.x][tn.y]+1 < F[tx.x][tx.y] || F[tx.x][tx.y] == 0)) {
J[tx.x][tx.y] = J[tn.x][tn.y]+1;
Q.push(tx);
}
}
}
puts("IMPOSSIBLE");
}
int main() {
int t, i, j;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
for(i = 0; i < n; i++) {
scanf("%s", g[i]);
for(j = 0; j < m; j++) {
if(g[i][j] == 'J')
pj.x = i, pj.y = j;
F[i][j] = 0, J[i][j] = 0;
}
}
sol();
}
return 0;
}

台長: Morris
人氣(789) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 12583 - Memory Overflow
此分類上一篇:[UVA][spfa] 11280 - Flying to Fredericton

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文