BFS Simple Maze

Source: Internet
Author: User

Common Maze:

Enter the end point of the maze and find the shortest path BFS example

Use dist[][] array to record the shortest path of the start point to each point

1#include <iostream>2#include <fstream>3#include <stdio.h>4#include <string.h>5#include <queue>6 using namespacestd;7 8 Const intMaxSize = -;9 Const intINF =0XFFF3;Ten intM,n; One intd[2][4] = { {-1,0,1,0}, {0,1,0, -1} }; A intDist[maxsize][maxsize];//Book Method: Use Dist[m][n] to store the shortest path initial INF indicates unreachable - structPos - { the     intx, y; - }st,ed; - CharMaze[maxsize][maxsize]; -  + BOOLCheckintXinty) - { +     if(X <0|| X >= m | | Y <0|| Y >= N)return false; A     if(Maze[x][y] = ='#'|| Dist[x][y]! = INF)return false;//!=inf said he'd been there before . at     return true; - } - intBFS () - { -      for(inti =0; I < m; i++) -        for(intj =0; J < N; J + +) inDIST[I][J] =INF; -queue<structPos>que; to Que.push (ST); +DIST[ST.X][ST.Y] =0;//myself to myself for 0 -      while(!que.empty ()) the     { *         intNX, NY; $Pos node =Que.front ();Panax Notoginseng Que.pop (); -         //ans + +;//the error here is not that every time the team is in the shortest path, the node debugs can be found . the         if(node.x = = Ed.x && node.y = = ed.y)returnDist[ed.x][ed.y]; +          for(inti =0; I <4; i++) A         { theNX = node.x + d[0][i]; +NY = node.y + d[1][i]; -             if(Check (NX, NY)) $             { $ Pos temp; -temp.x =NX; -TEMP.Y = NY;//It's better to use a pair here . theDist[nx][ny] = Dist[node.x][node.y] +1; - Que.push (temp);Wuyi             } the         } -     } Wu     return-1; - } About  $ intMain () - { -Ifstream Cin ("In.txt"); -Freopen ("In.txt","R", stdin); Ascanf"%d%d", &m, &n); + GetChar (); the      for(inti =0; i < m; i++) -     { $ gets (Maze[i]); the     } the      for(inti =0; I < m; i++) the     { the          for(intj =0; J < N; J + +) -         { in             if(Maze[i][j] = ='S') the             { theSt.x =i; AboutSt.y =J; the             } the             if(Maze[i][j] = ='G') the             { +Ed.x =i; -Ed.y =J; the             }Bayi         } the     } the     intAns =BFS (); -     if(ans = =-1) cout <<"No"<<Endl; -     Elsecout << ans <<Endl; the } the //to fill and memset also need to know is not in the loop fill the //record the shortest path with dist instead of every time out of the team ans++ the //time Complexity State transfer is four directions per lattice up to once O (4*m*n)

BFS Simple Maze

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.