uva 11624 FIRE!

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


题意很简单,先让火跑,然后记录把火燃烧的记录记在vf数组里面,然后再模拟人就行了,唯一有个以为就是,我的bfsF必须写在main函数里面才对,这一点没搞明白,如果有大佬感兴趣可以看一看,欢迎在下方评论,给我宝贵的意见。

先贴错误的代码,错误原因:答案错误

#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;
}

请大佬们指点一下,万分感谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值