uva 11624 Fire
Problem B: Fire!
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.
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.
Input Specification
The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 <= R,C <= 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of:
- #, a wall
- ., a passable square
- J, Joe's initial position in the maze, which is a passable square
- F, a square that is on fire
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 cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Output for Sample Input
3 IMPOSSIBLE
Malcolm Sharpe, Ondřej Lhoták
两个距离向量,分别用于记录各点着火最短距离(时间)、逃跑最短距离,两次BFS,然后比较哪些边界点后者小于前者即可,最后结果应该加1,表示走出边界点还需1单位时间。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<set> #include<queue> #include<string> #include<cmath> #include<fstream> #include<iomanip> #include<climits> #include<cfloat> using namespace std; #define MAX_INT 0x7fffffff #define MAX_LL 0x7fffffffffffffff #define ULL unsigned long long #define LL long long #define MAX(x,y) ((x) > (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MAXN 1111 int r,c; int d[MAXN][MAXN][2]; char g[MAXN][MAXN]; int jr,jc; struct cell{ int r,c; cell(int r0, int c0):r(r0),c(c0){} }; vector<cell> fires; queue<cell> q; int dr[]={-1,1,0,0}, dc[]={0,0,-1,1}; void bfs(int who){ while(!q.empty()){ cell t=q.front(); q.pop(); for(int i=0; i<4; i++){ int tr=t.r+dr[i],tc=t.c+dc[i]; if(tr>-1 && tr<r && tc>-1 && tc<c && g[tr][tc]=='.' && d[tr][tc][who]<0){ d[tr][tc][who]=d[t.r][t.c][who]+1; q.push(cell(tr, tc)); } } } } int ans; #define INF 10000000 void check(int x, int y){ int t1=d[x][y][0], t2=d[x][y][1]; if(t1<0) return; else if(t2<0) ans=MIN(ans,t1); else if(t1<t2) ans=MIN(t1,ans); } int main(){ //freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin); int cas; scanf(" %d",&cas); while(cas--){ int i,j; scanf(" %d %d",&r,&c); getchar(); fires.clear(); for(i=0; i<r; i++){ gets(g[i]); for(j=0; j<c; j++){ if(g[i][j]=='J') { g[i][j]='.'; jr=i; jc=j; } else if(g[i][j]=='F'){ fires.push_back(cell(i,j)); } } } memset(d, -1, sizeof(d)); d[jr][jc][0]=0; q.push(cell(jr,jc)); bfs(0); for(i=0; i<fires.size(); i++){ int tr=fires[i].r,tc=fires[i].c; d[tr][tc][1]=0; q.push(cell(tr,tc)); } bfs(1); ans=INF; for(i=0; i<r; i++) { check(i,0); check(i,c-1);} for(i=0; i<c; i++) { check(0,i); check(r-1,i);} if(ans==INF) printf("IMPOSSIBLE\n"); else printf("%d\n",1+ans); } return 0; }