UVA 11624

来源:互联网 发布:淘宝怎么查看消费记录 编辑:程序博客网 时间:2024/06/10 15:52
/*Joe works in a maze.  Unfortunately, portions of the maze havecaught on  re, and the owner of the maze neglected to create a  reescape plan. Help Joe escape the maze.Given Joe's location in the maze and which squares of the mazeare on  re, you must determine whether Joe can exit the maze beforethe  re reaches him, and how fast he can do it.Joe and the  re each move one square per minute, vertically orhorizontally (not diagonally).  The  re spreads all four directionsfrom each square that is on  re. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the  remay enter a square that is occupied by a wall.InputThe  rst line of input contains a single integer, the number of testcases to follow.  The  rst line of each test case contains the twointegersRandC, separated by spaces, with 1R;C1000. ThefollowingRlines of the test case each contain one row of the maze. Each of these lines contains exactlyCcharacters, 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  reThere will be exactly oneJin each test case.OutputFor each test case, output a single line containing `IMPOSSIBLE' if Joe cannot exit the maze before the re reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.SampleInput244#####JF##..##..#33####J.#.FSampleOutput3IMPOSSIBLE题目:一个平面迷宫中有一个人,迷宫中有些点起火了,火和人每个单位时间只能向相邻的格子移动,            其中有一些空间被墙壁占据,问这个人在不背或烧到的情况下,离开迷宫的最快时间。分析:搜索。迷宫中的最短路,首先就会想到bfs;并且bfs利用队列会使状态空间按时间顺序分层。            而火的扩散过程正好符合这个时间的层次。所以我们会想到,利用两个队列,一个储存人的状态,            一个储存火的状态。按照时间顺序,先更新火蔓延的节点,再扩展人能到达的节点。            通过分析,我们发现这两个队列可以合并,只须初始化的时候,按照火节点然后是人的顺序入队即可。           (节点中加入一个是否是火节点的判断,就可以两种节点按不同的细节处理)注意:时间是指走出迷宫的时间,到达边界后要加1;初始节点的判断。*/# include <iostream># include <cstdio># include <cstring># include <queue>using namespace std;const int MAX = 1010;int m, n;char map[MAX][MAX];int fire[MAX][MAX];     //火int people[MAX][MAX];   //人int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};queue<pair<int, int> > Q;void BFS_fire(){    memset(fire, -1, sizeof(fire));    while ( !Q.empty() )    {        Q.pop();    }    for (int i=0; i<n; i++)    {        for (int j=0; j<m; j++)        {            if (map[i][j] == 'F')            {                fire[i][j] = 0;                Q.push(make_pair(i, j));            }        }    }    while ( !Q.empty() )        //BFS火能烧到的地方,并且记录时间    {        pair<int, int> a;        a = Q.front();        Q.pop();        int x = a.first;        int y = a.second;        for (int i=0; i<4; i++)        {            int xx = x + dir[i][0];            int yy = y + dir[i][1];            if (xx<0 || xx>=n || yy<0 || yy>=m) continue;            if (fire[xx][yy] != -1) continue;        //被烧过            if (map[xx][yy] == '#') continue;       //有墙            fire[xx][yy] = fire[x][y] + 1;      //记录时间            Q.push(make_pair(xx, yy));        }    }}int BFS_people(){    memset(people, -1, sizeof(people));    while ( !Q.empty() )    {        Q.pop();    }    for (int i=0; i<n; i++)    {        for (int j=0; j<m; j++)        {            if (map[i][j] == 'J')            {                Q.push(make_pair(i, j));                people[i][j] = 0;            }        }    }    while ( !Q.empty() )    {        pair<int, int> a;        a = Q.front();        Q.pop();        int x = a.first;        int y = a.second;        if (x==0 || y==0 || x==n-1 || y==m-1)        {            return people[x][y]+1;        }        for (int i=0; i<4; i++)        {            int xx = x + dir[i][0];            int yy = y + dir[i][1];            if (xx<0 || xx>=n || yy<0 || yy>=m) continue;            if (people[xx][yy] != -1) continue;      //已经走过            if (map[xx][yy] == '#') continue;           //有墙            if (fire[xx][yy] != -1 && people[x][y]+1 >= fire[xx][yy]) continue;     //&&  之前是不能被火烧过  之后是到达的时间不能有火            people[xx][yy] = people[x][y] + 1;      //更新时间            Q.push(make_pair(xx, yy));        }    }    return -1;}int main(){    int t;    scanf("%d", &t);    while (t--)    {        scanf("%d %d", &n, &m);        //getchar();        for (int i=0; i<n; i++)        {            for (int j=0; j<m; j++)            {                cin >> map[i][j];            }        }        BFS_fire();        int ans = BFS_people();        if(ans == -1)        {            printf("IMPOSSIBLE\n");        }        else        {            printf("%d\n",ans);        }    }    return 0;}

0 0
原创粉丝点击