UVA - 11624:Fire!


Fire!
出典:UVA
ラベルらべる:図論ずろん
参考資料:
類似タイトル:
タイトル
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.
入力
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.

  • しゅつりょく
    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.
    入力サンプル
    2
    4 4
    ####
    #JF#
    #..#
    #..#
    3 3
    ###
    #J.
    #.F
    

    出力サンプル
    3 IMPOSSIBLE
    問題を解く構想.
    BFSで火が各場所に到達する時間を先に処理する.
    リファレンスコード
    #include
    #include 
    #include
    #define MAXN 1000
    using namespace std;
    char maze[MAXN+5][MAXN+5];
    int firetime[MAXN+5][MAXN+5];//init -1 each time
    int arrivetime[MAXN+5][MAXN+5];
    int vis[MAXN+5][MAXN+5];//init 0 each time
    int dx[4]={-1,0,1,0};
    int dy[4]={0,-1,0,1};
    int row,col;
    queue<int> qx,qy;
    
    void hasfire(){
    	while(!qx.empty()){
    		int x=qx.front();
    		int y=qy.front();
    		qx.pop();
    		qy.pop();
    		for(int i=0;i<4;i++){
    			int nx=x+dx[i];
    			int ny=y+dy[i];
    			if(nx>=0 && nx<row && ny>=0 && ny<col && !vis[nx][ny] && maze[nx][ny]!='#' ){
    				vis[nx][ny]=1;
    				firetime[nx][ny]=firetime[x][y]+1;
    				qx.push(nx);
    				qy.push(ny);
    			}
    		}
    	}
    }
    
    int escape(){
    	while(!qx.empty()){
    		int x=qx.front();
    		int y=qy.front();
    		qx.pop();
    		qy.pop();
    		if(x==0 || x==row-1 || y==0 || y==col-1){
    			return arrivetime[x][y]+1;
    		}
    		for(int i=0;i<4;i++){
    			int nx=x+dx[i];
    			int ny=y+dy[i];
    			if(nx>=0 && nx<row && ny>=0 && ny<col && !vis[nx][ny] && maze[nx][ny]=='.' ){
    				vis[nx][ny]=1;
    				if(firetime[nx][ny]==-1 || arrivetime[x][y]+1<firetime[nx][ny]){
    					arrivetime[nx][ny]=arrivetime[x][y]+1;
    					qx.push(nx);
    					qy.push(ny);
    					
    				}
    			}
    		}
    	}
    	return -1;
    }
    
    void clearqueue(){
    	while(!qx.empty()){
    		qx.pop();
    		qy.pop();
    	}
    }
    
    int main(){
    	int T;
    	scanf("%d",&T);
    	while(T--){
    		memset(firetime,-1,sizeof(firetime));
    		memset(arrivetime,-1,sizeof(arrivetime));
    		memset(vis,0,sizeof(vis));
    		clearqueue();
    		scanf("%d%d",&row,&col);
    		for(int i=0;i<row;i++){
    			scanf("%s",maze[i]);
    		}
    		int jx,jy;
    		for(int i=0;i<row;i++){
    			for(int j=0;j<col;j++){
    				if(maze[i][j]=='F'){
    					qx.push(i);
    					qy.push(j);
    					firetime[i][j]=0;
    					vis[i][j]=1;
    				}
    				if(maze[i][j]=='J'){
    					jx=i;
    					jy=j;
    				}
    			}
    		}
    		hasfire();
    		
    		memset(vis,0,sizeof(vis));
    		clearqueue();
    		qx.push(jx);
    		qy.push(jy);
    		arrivetime[jx][jy]=0;
    		vis[jx][jy]=1;
    		int ans=escape();
    		if(ans==-1){
    			printf("IMPOSSIBLE
    "
    ); }else{ printf("%d
    "
    ,ans); } } return 0; }