uva 11624 FIRE!

来源:互联网 发布:电视机用网络看电视 编辑:程序博客网 时间:2024/05/17 23:18

Problem Description:

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 squareJ, Joe's initial position in the maze, which is a passable squareF, a square that is on fireThere will be exactly one J in each test case.**Sample Input**24 4#####JF##..##..#3 3####J.#.F**Output Specif**> 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.**Simple Output**3IMPOSSIBLE**问题分析:**简单图论问题~~先宽搜一回求出每个格子着火的时间然后再宽搜一回求最短路。。。不过要注意细节哦!
#include<iostream>#include<queue>#include<cstring>#define F(a,b) for(int a = 0 ; a < (b); a++ )using namespace std;typedef struct point p;struct point{    int x;    int y;    int d;    bool operator <(const point &a)const{return d<a.d;}};int m,n;int line[4] = {0,0,1,-1};int lie[4] = {1,-1,0,0};int x0,y0;int map[1010][1010];int t[1010][1010];queue<p>f;bool inside(int x,int y){    return x<m&&x>=0&&y<n&&y>=0;}void bfs1(){    while(!f.empty())    {        p& fro = f.front();        f.pop();        F(i,4)        if(inside(fro.x+line[i],fro.y+lie[i])&&map[fro.x+line[i]][fro.y+lie[i]]==0&&t[fro.x+line[i]][fro.y+lie[i]]==-1)        {            t[fro.x+line[i]][fro.y+lie[i]] = t[fro.x][fro.y] + 1;            f.push((p){fro.x+line[i],fro.y+lie[i],0});          }    }}int bfs2(){    queue<p>q;    q.push((p){x0,y0,0});    while(!q.empty())    {        p& xx = q.front();        q.pop();        if(xx.x==0||xx.x==m-1||xx.y==0||xx.y==n-1)return xx.d+1;        F(i,4)        if(inside(xx.x+line[i],xx.y+lie[i])&&map[xx.x+line[i]][xx.y+lie[i]]==0&&xx.d+1<t[xx.x+line[i]][xx.y+lie[i]])        {            q.push((p){xx.x+line[i],xx.y+lie[i],xx.d+1});        }    }    return 0;}int main(){    memset(t,-1,sizeof(t));    int T;    cin >> T;    while(T--)    {        cin >> m >> n;        F(i,m)F(j,n)        {            char ch;            cin >> ch;            if(ch == '#')map[i][j]=-1;            if(ch == 'F'){map[i][j]=1;f.push((p){i,j,0});t[i][j] = 0;}            if(ch == 'J'){map[i][j]==0;x0=i;y0=j;}            if(ch == '.')map[i][j]==0;        }        bfs1();        int ans = bfs2();        if(ans)cout << ans;        else cout << " IMPOSSIBLE" ;        if(T!=0)cout << endl;    }    return 0;}
0 0
原创粉丝点击