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.
2 4 4 #### #JF# #..# #..# 3 3 ### #J. #.F
3 IMPOSSIBLE
题目大意:
有个人被困于火场中。火焰每分钟蔓延1格,人每分钟移动一格,求出人是否会被困于火场,或者逃脱,如果逃脱,求出最短的时间。
其中
'#'表示墙壁,'F'表示火焰,'J'表示人的初始位置,'.'表示道路。
注意:不要被样例给误导了,可能刚开始火焰不止一处。
解析:
有两种解法。
解法一,
先对火BFS一次,求出每个点的最小着火时间,并用二维数组保存。
再对人BFS一次,求出走到边界的最少时间,每次如果移动的时间>=该点着火的时间,则continue。
解法二,
用一个结构体标记人或者是火,,先将火和人的位置分别bfs(先火再人),如果是火就bfs该点并用'F'占位,如果是人只能移动在'.'上,直到人到达边界或是被火围住无路可走时结束。
解法一
#include
#include
#include
#include
using namespace std;
struct Node {
int r,c,lev;
Node(int r,int c,int lev) {
this->r = r;
this->c = c;
this->lev = lev;
}
};
const int INF = 0x3f3f3f;
const int N = 1010;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = { 0,-1, 0, 1};
int m,n;
char grid[N][N];
int vis[N][N];
int fire[N][N];
void bfs_fire() {
memset(vis,0,sizeof(vis));
queue q;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(grid[i][j] == 'F') {
vis[i][j] = true;
fire[i][j] = 0;
q.push(Node(i,j,0));
}
}
}
int r,c,lev;
while( !q.empty() ) {
Node front = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
r = front.r + dr[i];
c = front.c + dc[i];
lev = front.lev + 1;
if( r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == '#' || grid[r][c] == 'F' || vis[r][c]) {
continue;
}
vis[r][c] = true;
fire[r][c] = lev;
q.push(Node(r,c,lev));
}
}
}
int bfs(int sr,int sc) {
memset(vis,0,sizeof(vis));
queue q;
q.push(Node(sr,sc,0));
vis[sr][sc] = true;
int r,c,lev;
while( !q.empty() ) {
Node front = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
r = front.r + dr[i];
c = front.c + dc[i];
lev = front.lev + 1;
if( r < 0 || r >= m || c < 0 || c >= n ) {
return lev;
}
if(fire[r][c] <= lev || grid[r][c] == '#' || grid[r][c] == 'F' || vis[r][c] ) {
continue;
}
vis[r][c] = true;
q.push(Node(r,c,lev));
}
}
return 0;
}
int main() {
int t;
int sr,sc;
while( scanf("%d",&t) != EOF) {
while( t-- ) {
memset(grid,0,sizeof(grid));
memset(fire,0,sizeof(fire));
scanf("%d%d",&m,&n);
for(int i = 0; i < m; i++) {
scanf("%s",grid[i]);
for(int j = 0; j < n; j++) {
fire[i][j] = INF; //初始化为最大
if(grid[i][j] == 'J') {
sr = i;
sc = j;
}
}
}
bfs_fire();
int cnt = bfs(sr,sc);
if(cnt) {
printf("%d\n",cnt);
}else {
printf("IMPOSSIBLE\n");
}
}
}
return 0;
}
解法二
#include
#include
#include
#include
using namespace std;
struct Node{
int r,c,lev;
bool fire;
Node(int r,int c,int lev,bool fire) {
this->r = r;
this->c = c;
this->lev = lev;
this->fire = fire;
}
};
const int dr[] = {-1, 0, 1, 0};
const int dc[] = { 0,-1, 0, 1};
const int N = 1010;
char grid[N][N];
int vis[N][N];
int m,n;
Node* q[N*N];
int bfs(int sr,int sc) {
int front,rear;
front = rear = 0;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(grid[i][j] == 'F') {
q[rear++] = new Node(i,j,0,true);
}
}
}
q[rear++] = new Node(sr,sc,0,false);
vis[sr][sc] = true;
int r,c,lev;
while(front < rear) {
Node* pre = q[front++];
if(pre->fire) {
for(int i = 0; i < 4; i++) {
r = pre->r + dr[i];
c = pre->c + dc[i];
lev = pre->lev + 1;
if(r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == '#' || grid[r][c] == 'F') {
continue;
}
grid[r][c] = 'F';
q[rear++] = new Node(r,c,lev,true);
}
}
else {
for(int i = 0; i < 4; i++) {
r = pre->r + dr[i];
c = pre->c + dc[i];
lev = pre->lev + 1;
if(grid[r][c] == '#' || grid[r][c] == 'F' || vis[r][c]) {
continue;
}
if(r < 0 || r >= m || c < 0 || c >= n) {
return lev;
}
vis[r][c] = true;
q[rear++] = new Node(r,c,lev,false);
}
}
}
return 0;
}
int main() {
int t;
int sc,sr;
while( scanf("%d",&t) != EOF) {
while(t--) {
memset(grid,0,sizeof(grid));
memset(vis,0,sizeof(vis));
//memset(q,NULL,sizeof(q));
scanf("%d%d",&m,&n);
for(int i = 0; i < m; i++) {
scanf("%s",grid[i]);
for(int j = 0; j < n; j++) {
if(grid[i][j] == 'J') {
sr = i;
sc = j;
}
}
}
int cnt = bfs(sr,sc);
if(cnt) {
printf("%d\n",cnt);
}
else {
printf("IMPOSSIBLE\n");
}
}
}
return 0;
}