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
先贴错误的代码,错误原因:答案错误
#include <queue>
#include <stdio.h>
#include <string.h>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
int R , C;
int vf[1005][1005];
int vj[1005][1005];
char Map[1005][1005];
int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
struct node{
int x , y;
int f;
}pre,nex;
bool MAP(int x,int y) {
return x>=0 && y>=0 && x<R && y<C;
}
bool escape(int x,int y) {
return x==0 || y==0 || x==R-1 || y==C-1;
}
void bfsF(int x, int y) {
queue<node> q;
pre.x = x;
pre.y = y;
pre.f = 0;
vf[x][y] = pre.f;
q.push(pre);
while(!q.empty()) {
nex = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
int dx = d[i][0] + nex.x;
int dy = d[i][1] + nex.y;
if(MAP(dx,dy) && Map[dx][dy] != '#' && vf[dx][dy] == inf) {
pre.x = dx;
pre.y = dy;
pre.f = nex.f+1;
vf[dx][dy] = pre.f;
q.push(pre);
}
}
}
return ;
}
void bfsJ(int x,int y) {
queue<node> q;
memset(vj, 0, sizeof(vj));
pre.x = x;
pre.y = y;
pre.f = 0;
vj[x][y] = 1;
q.push(pre);
while(!q.empty()) {
nex = q.front();
q.pop();
if(escape(nex.x , nex.y)) {
printf("%d\n",nex.f+1);
return ;
}
for(int i = 0; i < 4; i++) {
int dx = d[i][0] + nex.x;
int dy = d[i][1] + nex.y;
if(MAP(dx, dy) && Map[dx][dy] != '#' && vj[dx][dy] == 0) {
if(nex.f+1 < vf[dx][dy]) {
pre.x = dx;
pre.y = dy;
pre.f = nex.f+1;
vj[dx][dy] = 1;
q.push(pre);
}
}
}
}
puts("IMPOSSIBLE");
return ;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
int fx, fy, jx, jy;
scanf("%d%d",&R,&C);
for(int i = 0; i < R; i++) {
scanf("%s",Map[i]);
for(int j = 0; j < C; j++) {
if(Map[i][j] == 'F') {
fx = i;
fy = j;
}
if(Map[i][j] == 'J') {
jx = i;
jy = j;
}
}
}
memset(vf, inf, sizeof(vf));
bfsF(fx , fy);
bfsJ(jx , jy);
}
return 0;
}
下面是正确的代码:
#include <queue>
#include <stdio.h>
#include <string.h>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
int R , C;
int vf[1005][1005];
int vj[1005][1005];
char Map[1005][1005];
int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
struct node{
int x , y;
int f;
}pre,nex;
bool MAP(int x,int y) {
return x>=0 && y>=0 && x<R && y<C;
}
bool escape(int x,int y) {
return x==0 || y==0 || x==R-1 || y==C-1;
}
void bfsJ(int x,int y) {
queue<node> q;
memset(vj, 0, sizeof(vj));
pre.x = x;
pre.y = y;
pre.f = 0;
vj[x][y] = 1;
q.push(pre);
while(!q.empty()) {
nex = q.front();
q.pop();
if(escape(nex.x , nex.y)) {
printf("%d\n",nex.f+1);
return ;
}
for(int i = 0; i < 4; i++) {
int dx = d[i][0] + nex.x;
int dy = d[i][1] + nex.y;
if(MAP(dx, dy) && Map[dx][dy] != '#' && vj[dx][dy] == 0) {
if(nex.f+1 < vf[dx][dy]) {
pre.x = dx;
pre.y = dy;
pre.f = nex.f+1;
vj[dx][dy] = 1;
q.push(pre);
}
}
}
}
puts("IMPOSSIBLE");
return ;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
queue<node> q;
int fx, fy, jx, jy;
scanf("%d%d",&R,&C);
for(int i = 0; i < R; i++) {
scanf("%s",Map[i]);
for(int j = 0; j < C; j++) {
if(Map[i][j] == 'F') {
pre.x = i;
pre.y = j;
pre.f = 0;
vf[i][j] = pre.f;
q.push(pre);
}
if(Map[i][j] == 'J') {
jx = i;
jy = j;
}
}
}
memset(vf, inf, sizeof(vf));
while(!q.empty()) {
nex = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
int dx = d[i][0] + nex.x;
int dy = d[i][1] + nex.y;
if(MAP(dx,dy) && Map[dx][dy] != '#' && vf[dx][dy] == inf) {
pre.x = dx;
pre.y = dy;
pre.f = nex.f+1;
vf[dx][dy] = pre.f;
q.push(pre);
}
}
}
bfsJ(jx , jy);
}
return 0;
}
请大佬们指点一下,万分感谢。