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

 

//============================================================================
// Name        : UVA11624.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN=1010;
int n,m;
char g[MAXN][MAXN];
queue<pair<int,int> >q;
int a[MAXN][MAXN];
int move[][2]={{0,1},{0,-1},{1,0},{-1,0}};
void bfs1()
{
    memset(a,-1,sizeof(a));
    while(!q.empty())q.pop();
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(g[i][j]=='F')
            {
                a[i][j]=0;
                q.push(make_pair(i,j));
            }
    while(!q.empty())
    {
        pair<int,int> tmp=q.front();
        q.pop();
        int x=tmp.first;
        int y=tmp.second;
        for(int i=0;i<4;i++)
        {
            int tx=x+move[i][0];
            int ty=y+move[i][1];
            if(tx<0||ty<0||tx>=n||ty>=m)continue;
            if(a[tx][ty]!=-1)continue;
            if(g[tx][ty]=='#')continue;
            a[tx][ty]=a[x][y]+1;
            q.push(make_pair(tx,ty));
        }
    }
}
int b[MAXN][MAXN];
int bfs2()
{
    memset(b,-1,sizeof(b));
    while(!q.empty())q.pop();
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(g[i][j]=='J')
            {
                q.push(make_pair(i,j));
                b[i][j]=0;
            }
    while( !q.empty() )
    {
        pair<int,int> tmp=q.front();
        q.pop();
        int x=tmp.first;
        int y=tmp.second;
        if(x==0 || y==0 || x==n-1 || y==m-1)
            return b[x][y]+1;
        for(int i=0;i<4;i++)
        {
            int tx=x+move[i][0];
            int ty=y+move[i][1];
            if(tx<0||ty<0||tx>=n||ty>=m)continue;
            if(b[tx][ty]!=-1)continue;
            if(g[tx][ty]=='#')continue;
            if(a[tx][ty]!=-1 && b[x][y]+1>=a[tx][ty])continue;
            b[tx][ty]=b[x][y]+1;
            q.push(make_pair(tx,ty));
        }
    }
    return -1;
}
int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%s",g[i]);
        bfs1();
        int ans=bfs2();
        if(ans==-1)printf("IMPOSSIBLE\n");
        else printf("%d\n",ans);
    }
    return 0;
}

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM