poj3984 Maze problem (how to output the shortest circuit)

Source: Internet
Author: User

Title: Maze Problem

Time limit:1000ms Memory limit:65536kb 64bit IO format:%lld &%llu

Description
Define a two-dimensional array:

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

It represents a maze, of which 1 represents a wall, 0 means that the road can be walked, can only walk sideways or vertical walk, can not be inclined to walk, asked to compile the program to find the shortest route from the upper left to the lower right corner.

Input
A 5x5 two-dimensional array that represents a maze. The data guarantee has a unique solution.

Output
The shortest path in the upper-left corner to the lower-right corner, formatted as shown in the sample.

Sample Input
0 1 0) 0 0
0 1 0) 1 0
0 0 0) 0 0
0 1 1) 1 0
0 0 0) 1 0

Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

Test instructions
Test instructions is very simple, from the upper left to the lower right corner of the shortest

Ideas:
BFS, get the shortest circuit, the problem is how to output the quickest way. Open an array of node type Pre[x][y] and record (x, y) which point the precursor of this point is. We can think about it when we're in the queue, suppose at some point there are two paths to choose, respectively A, B, C, D and E, F, G, H, then in the queue, a queue, E queue, then a out team, B queue, then the precursor of B is a,e out team, F queue, F's precursor is e, not difficult to understand, the precursor can save the path. So we can get the answer by reversing the output when we know the end position. Well, how to reverse output it. Write a stack, the path is wild, I compare low

Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <

vector> #include <queue> #include <stack> using namespace std;


struct node{int x, y;};
int arr[5][5];
BOOL Vis[5][5];
int dx[4] = {0, 1,-1, 0};
int dy[4] = {1, 0, 0,-1};


Node pre[10][10];
    void print () {stack<node> Zhan;

    Node lrh{4, 4};
        while (1) {Zhan.push (LRH);
        if (lrh.x==0 && lrh.y==0) break;
    LRH = Pre[lrh.x][lrh.y]; } while (!zhan.empty ()) {cout<< "(" <<zhan.top () .x<< "," <<zhan.top () .y<< ")" <&lt
        ; Endl;
    Zhan.pop ();
    }} void BFs (int x, int y) {queue<node> q;
    Q.push (node{x, y});
    Vis[x][y] = true;
        while (!q.empty ()) {node TMP = Q.front ();
        Q.pop ();
        if (tmp.x==4&&tmp.y==4) {print (); return;}
            for (int i=0; i<4; i++) {int now_x = tmp.x + dx[i]; int now_y = TMP.Y + dy[i]; if (0<=now_x && now_x<5 && 0<=now_y && now_y<5 &&!vis[now_x][now_y] && Amp
                arr[now_x][now_y]==0) {Q.push (node{now_x, now_y});
                PRE[NOW_X][NOW_Y] = tmp;
            Vis[now_x][now_y] = true;
    }}}} int main () {//freopen ("Input.txt", "R", stdin);
    Freopen ("Output.txt", "w", stdout);
    memset (arr, 0, sizeof (arr));

    for (int i=0, i<5; i++) for (int j=0; j<5; j + +) cin>>arr[i][j];
    Memset (Vis, false, sizeof (VIS));

    memset (pre, 0, sizeof (pre));

    BFS (0, 0);
return 0; }

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.