题目大意:有一个迷宫着火了,这个迷宫里面有个人,这个人想要逃出迷宫,求最小的逃出距离是多少。人和火的走动的方向只有四个方向,上下左右
解题思路:要判断下一个点能不能走,就得先判断一下走下去后火会不会烧过来,所以在每次走之前,要先让火走,再让人走
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 1010
typedef struct status{
int x,y,s,f;
status(int X,int Y, int S, int F) {
this->x = X;
this->y = Y;
this->s = S;
this->f = F;
}
status() {}
}point;
char maze[maxn][maxn];
int vis[maxn][maxn];
point p[maxn*maxn];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
void BFS(int R, int C) {
int move = 0, F_move = 0;
point J;
for(int i = 0; i < R; i++)
for(int j = 0; j < C; j++) {
if(maze[i][j] == 'J')
J = point(i,j,0,0);
if(maze[i][j] == 'F')
p[F_move++] = point(i,j,0,1);
vis[i][j] = 0;
}
vis[J.x][J.y] = 1;
p[F_move++] = J;
while(move < F_move) {
point temp, now = p[move];
if(!now.f) {
if(now.x == 0 || now.x == R - 1|| now.y == 0 || now.y == C-1) {
printf("%d\n",now.s+1);
return ;
}
}
for(int i = 0; i < 4; i++) {
temp.x = now.x + dir[i][0];
temp.y = now.y + dir[i][1];
temp.s = now.s + 1;
temp.f = now.f;
if(temp.x >= 0 && temp.x < R && temp.y >= 0 && temp.y < C) {
if(maze[temp.x][temp.y] == '.') {
if(temp.f) {
maze[temp.x][temp.y] = 'F';
p[F_move++] = temp;
}
else if(!vis[temp.x][temp.y]) {
p[F_move++] = temp;
vis[temp.x][temp.y] = 1;
}
}
}
}
move++;
}
printf("IMPOSSIBLE\n");
}
int main() {
int test,R,C;
while(scanf("%d",&test) != EOF ) {
while(test--) {
scanf("%d%d",&R, &C);
for(int i = 0; i < R; i++)
scanf("%s",maze[i]);
BFS(R,C);
}
}
return 0;
}