POJ2935 Basic Wall Maze BFS Record path

Source: Internet
Author: User

Links: POJ2935




Test instructions

6 X 6 Map lattices and grids may have walls the entire map has three walls for the path from the beginning to the end of the road


The wall in the subject can be understood as the X-direction of a position cannot walk, and a three-dimensional array map[x][y][z] indicates that the z direction of (x, y) cannot go

About the record path you can use a pre array to record the compound value of the previous coordinate of each coordinate in the final reverse direction of the output


Code

#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std;               struct node{int x, y,} head;int pre[10][10];int Vis[10][10];bool map[7][7][4];                  Realizing the function of the wall int dir[4][2]= {1,0,0,1,-1,0,0,-1};char fx[3][3];    Save Direction name int startx,starty,endx,endy;void BFS () {queue<node>q;    Vis[head.x][head.y]=1;    Q.push (head);        while (!q.empty ()) {Head=q.front ();        Q.pop ();        if (Head.x==endx&&head.y==endy) {return;}        int tx,ty;            for (int i=0; i<4; i++) {tx=head.x+dir[i][0];            TY=HEAD.Y+DIR[I][1]; if (tx<1| | ty<1| | tx>6| | ty>6| | vis[tx][ty]| |            Map[head.x][head.y][i]) continue;            Vis[tx][ty]=1;   Pre[tx][ty]=head.x*7+head.y;        The load coordinate of the previous coordinate Q.push ({tx,ty});    }}}void output () {int i,j=0;    int tx,ty;    Char q[49];     fx[1][0]= ' E '; fx[0][1]= ' S '; fx[1][2]= ' W '; fx[2][1]= ' N ';    X y plus 1 avoid crossing while (Endx!=startx&&endy!=starty) {TX=PRE[ENDX][ENDY]/7;          ty=pre[endx][endy]%7;        Decrypt the previous coordinate q[j++]=fx[tx-endx+1][ty-endy+1];    Endx=tx;endy=ty;        } for (j=j-2;j>=0;j--) cout<<q[j];    cout<<endl; return;} int main () {while (scanf ("%d%d", &head.y,&head.x) && (head.x!=0)) {scanf ("%d%d", &endy,&e        NDX);        memset (map,false,sizeof (map));        memset (pre,0,sizeof (pre));        memset (vis,0,sizeof (VIS));        int i,j=3,ax,ay,bx,by,t;            while (j--)//Implement wall function {scanf ("%d%d%d%d", &AMP;AY,&AMP;AX,&AMP;BY,&AMP;BX);            if (ay>by) {t=ay;ay=by;by=t;}            if (AX&GT;BX) {t=ax;ax=bx;bx=t;}                    if (Ay==by) {for (i=ax+1; i<=bx; i++) {map[i][ay][1]=1;                Map[i][ay+1][3]=1;        }            }    if (AX==BX) {for (i=ay+1; i<=by; i++) {map[ax][i][0]=1;                Map[ax+1][i][2]=1;        }}} BFS ();    Output (); } return 0;}


POJ2935 Basic Wall Maze BFS Record path

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.