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
解体思路:只想说我知道火的个数是未知的,但我还是先WA到死,然后是超时,有这么悲崔的吗?.我一开始是让J先遍历,然后再一一遍历火.给几个这个思路WA的样例,大家可以参考:
2
3 3
.F.
FJF
.F.
3 3
###
#J#
###
IMPOSSIBLE
IMPOSSIBLE
后来换了一个思路,让火都入队,同时遍历,求出每个位置火到达的最小时间,最后遍历J,这样就省了很多时间。而且需要讨论的情况也变少了。
AC代码如下:
#include
#include
#define INF 0x3f3f3f3f
#include
using namespace std;
int vist[1010][1010];
char mark[1010][1010];
int fire[1010][1010];
int n,m;
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct stu{
int x,y,t;
};
queueq;
void bfs1(){
stu temp,star;
while(!q.empty()){
star=q.front();
q.pop();
for(int i=0;i<4;i++){
temp.x=star.x+dis[i][0];
temp.y=star.y+dis[i][1];
temp.t=star.t+1;
if(temp.x<0||temp.y<0||temp.x>=n||temp.y>=m)continue;
if(vist[temp.x][temp.y])continue;
if(mark[temp.x][temp.y]=='#')continue;
if(temp.t=n||temp.y>=m)continue;
if(vist[temp.x][temp.y])continue;
if(mark[temp.x][temp.y]=='#')continue;
if(temp.t>=fire[temp.x][temp.y])continue;
vist[temp.x][temp.y]=1;
q.push(temp);
}
}
return 0;
}
int main(){
int t,x,y;
stu node;
scanf("%d",&t);
while(t--){
memset(vist,0,sizeof(vist));
while(!q.empty()) q.pop();
scanf("%d%d",&n,&m);
for(int i=0;i
代码如下:
#include
#include
#include
#define INF 0x3f3f3f3f
int n,m,ans,val;
using namespace std;
char mark[1010][1010];
int time[1010][1010];
int vist[1010][1010];
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct stu{
int x,y,t;
};
bool check(stu temp){
if(temp.x<0||temp.y<0||temp.x>=n||temp.y>=m)return false;
else if(vist[temp.x][temp.y])return false;
else if(mark[temp.x][temp.y]=='#')return false;
return true;
}
void bfs(int x,int y,int k){
queueq;
memset(vist,0,sizeof(vist));
while(!q.empty()){
q.pop();
}
stu temp,star;
temp.x=x;temp.y=y;temp.t=1;
vist[x][y]=1;
if(!k)
time[x][y]=1;
q.push(temp);
while(!q.empty()){
star=q.front();
q.pop();
for(int i=0;i<4;i++){
temp.x=star.x+dis[i][0];
temp.y=star.y+dis[i][1];
if(check(temp)){
if(!k&&mark[temp.x][temp.y]=='F')continue;
temp.t=star.t+1;
vist[temp.x][temp.y]=1;
if(!k){
time[temp.x][temp.y]=temp.t;
if(temp.x==0||temp.y==0||temp.x==n-1||temp.y==m-1){
if(temp.ttime[temp.x][temp.y]&&(temp.x==0||temp.y==0||temp.x==n-1||temp.y==m-1)){
ans=time[temp.x][temp.y];
}
q.push(temp);
}
}
}
}
int main(){
int t;
int x,y;
scanf("%d",&t);
while(t--){
int cas=0;
scanf("%d%d",&n,&m);
for(int i=0;imax)max=ans;
}
}
}
}
if(flag||val>=INF) printf("IMPOSSIBLE\n");
else if(flag==0&&max==-1)printf("%d\n",val);
else if(flag==0&&max!=-1)printf("%d\n",max);
}
return 0;
}