HLG 1613 Maze problem (BFS + priority_queue)

Source: Internet
Author: User

Links: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1613


Description:
Small z is in a maze, and small z can walk to one of the adjacent lattices in four directions, up and down, every minute. There are some walls and obstacles in the maze.
At the same time, there are some portals in the maze, and when small z goes to any of the portals, it is possible to transfer them to any other portal (the transmission is not time consuming).
Of course you can stay where you are. Now Little Z wants to know the minimum time it takes to get out of the maze.

Input:
Enter the number of first behavior groups T (t<=10).
For each set of data, the first behavior is two integers R and C (1<=r,c<=100). The following r lines have c characters per line, which is the maze map.
Where "#" stands for walls and obstacles, "." Indicates the open space, "P" represents the portal, "Z" represents the starting position of small Z, "W" indicates the Labyrinth exit.
For each set of data the starting position and the labyrinth exit are guaranteed to be unique.

Output:
For each set of data, the output is the shortest time to go out of the maze (in minutes). If you can't get out of the maze, output "impossible".


Sample Input:
2
3 4
. Z..
. p#.
# #PW
4 4
Z.. P
....
##..
w#. P

Sample Output:
2
Impossible

Analytical:

BFS + priority_queue

The code resolves as follows:

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <set> #include <queue> #include <algorithm> #define MAXN 205#define RST (n) memset (n, 0,    sizeof (N)) using namespace std;typedef struct _node{int x, y; int step;} Node; Node P[MAXN], Start;char map[maxn][maxn];int v[maxn][maxn];int N, M, res, Cas;int Sx, Sy, Ex, Ey, k;const int dx[] = {-1, 0, 1, 0};const int dy[] = {0, 1, 0,-1};p riority_queue <Node> pq;bool operator < (Node A, Node B) {return a.st EP > B.step;} BOOL Check (int x, int y) {return x>=0 && y>=0 && x<n && y<m && map[x][y]!= ' # ' &&!v[x][y];}    int BFS () {Node cur;    int px, py, xx, yy, cstep;        while (!pq.empty ()) {cur = pq.top (), Pq.pop ();        px = cur.x, py = cur.y, cstep = Cur.step;            for (int i=0; i<4; i++) {xx = px + dx[i], yy = py + dy[i];            if (check (xx, yy)) {    if (xx = = Ex && yy = = Ey) return cstep+1; if (map[xx][yy] = = ' P ') {for (int j=0; j<k; J + +) {//fly to the location of the portal where no other access has been visited;                        T FX = p[j].x, fy = p[j].y;                            if (!v[fx][fy]) {cur.x = FX, cur.y = FY;                            Cur.step = Cstep + 1;                        Pq.push (cur); }}}else if (map[xx][yy] = = '. ')                    {cur.x = xx, cur.y = yy;                    Cur.step = Cstep + 1;                Pq.push (cur);            } V[xx][yy] = 1; }}} return-1;}    int main () {scanf ("%d", &cas);        while (cas--) {scanf ("%d%d", &n, &m);        k = 0;            for (int i=0; i<n; i++) {scanf ("%s", Map[i]);                for (int j=0; j<m; J + +) {if (map[i][j] = = ' Z ') {sx=i, sy=j;} else if (map[i][j] = = 'W ') {ex=i, ey=j;}                    else if (map[i][j] = = ' P ') {//record the position of the portal p[k].x = i, p[k].y = j;                P[k++].step = 0;        }}} RST (v);        V[sx][sy] = 1;        while (!pq.empty ()) Pq.pop ();        Start.x = Sx, Start.y = Sy, start.step = 0;        Pq.push (start);        res = BFS ();        if (res! =-1) printf ("%d\n", res);    else printf ("impossible\n"); } return 0;}

HLG 1613 Maze problem (BFS + priority_queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.