Maze of Genius (bfs+ record Path)

Source: Internet
Author: User

1 //to find the shortest distance from (sx.sy) to (gx.gy);2 3#include <iostream>4#include <cstdio>5#include <cstdio>6#include <queue>7#include <cstring>8 #defineINF 999999999 Ten using namespacestd; One  Atypedef pair<int,int> P;//number of pairs, record location - intN,m,cur;//Scale Size - intSx,sy;//Start coordinates the intGx,gy;//End coordinates - intdis[501][501];///an array of the shortest distances to each location; - intmaze[501][501];//Show Maze - intdx[4]={1,0,-1,0};//Vector in four directions + intdy[4]={0,1,0,-1};//Vector in four directions - structnode{ +     intx, y; A}pre[501][501];//Recording precursor at  -  - intBFS ()//Breadth Search - { -Queue<p>que;  -      for(intI=1; i<=n;i++) in          for(intj=1; j<=m;j++) -dis[i][j]=INF; toQue.push (P (1,1));//Join the start queue +dis[1][1]=0;//added queue distance has, the representative visited the -      the     //keep looping until the queue length is 0 *      while(Que.size ()) $     {Panax NotoginsengP p=Que.front (); Que.pop (); -         if(p.first==n&&p.second==m) the              Break; +          for(intI=0;i<4; i++)//Four Directions Access A         { the             intNx=p.first+dx[i],ny=p.second+dy[i];//represents the point that will be visited +              -             if(1<=nx&&nx<=n&&1<=ny&&ny<=m&&maze[nx][ny]==0&&dis[nx][ny]==INF) $             { $Que.push (P (Nx,ny)); dis[nx][ny]=dis[p.first][p.second]+1;//Qualifying Join Queue -Pre[nx][ny].x=p.first; Pre[nx][ny].y=p.second;//Recording precursor -             } the              -         }Wuyi     } the     returnDis[n][m]; -      Wu } - voidPrint_road (intXintY//Write a precursor About { $     if(pre[x][y].x==-1) -     return ; - print_road (PRE[X][Y].X,PRE[X][Y].Y); -     if(x==pre[x][y].x&&y>pre[x][y].y) Aprintf"R"); +     Else if(x>pre[x][y].x&&y==pre[x][y].y) theprintf"D"); -     Else    if(x<pre[x][y].x&&y==pre[x][y].y) $printf"U"); the     Else     if(x==pre[x][y].x&&y<pre[x][y].y) theprintf"L"); the } the intMain () - { in     //int n,m; the      while(~SCANF ("%d%d",&n,&m)) the     { Aboutpre[1][1].x=pre[1][1].y=-1; the          for(intI=1; i<=n;i++) the              for(intj=1; j<=m;j++) thescanf"%1d",&maze[i][j]); +Cur=BFS (); -printf"%d\n", cur); thePrint_road (N,M);//Print PathBayi     } the}
bfs+ Record precursor

Maze of Genius (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.