Use BFS to solve the Maze problem and BFS to solve the Maze problem

Source: Internet
Author: User

Use BFS to solve the Maze problem and BFS to solve the Maze problem
In an n * n matrix, the path starts from the origin point (0, 0) to the end point (n-1, n-1). The path can only go in the upper, lower, and left directions, but only in the given matrix, calculate the minimum number of steps. N * n is the 01 matrix. 0 indicates that the grid has no obstacle. 1 indicates that there is an obstacle. Int mazeArr [maxn] [maxn]; // represents the 01 matrix int stepArr [4] [2] = {-}, {}, {0, -1 },{ }}; // indicates int visit [maxn] [maxn] in the upper, lower, and left directions; // indicates whether the vertex has been accessed to prevent backtracking, tracing is time-consuming. Solution: BFS finds the shortest path. If DFS is used, it is not the shortest path. We first access the origin from top to bottom to the left. If the top and bottom points are not obstacles and have not been accessed, we will put these points into the queue and set them to accessed, then, the nodes are output to the queue sequentially, and the previous steps are repeated to access the nodes from top to bottom, left, and right .... If the last access ends with (n-1, n-1), the end code is as follows:

# Include <iostream> using namespace std; // defines the rows and columns of the Maze # define ROW_COL 4 // defines a struct, which indicates the path through which struct Node {int x; int y; int step;}; // value Node fuzhi (int x, int y, int step) {Node p; p. x = x; p. y = y; p. step = step; return p ;} // implement a cyclic queue. ====## define QueueSize 30 typedef struct {Node Seq [QueueSize]; int front; int rear; int count;} RQueue; RQueue Q; void Initiate_Queue (RQueue * Q) {Q-> front = 0; Q-> rear = 0; Q-> Count = 0;} void AppendQueue (RQueue * Q, Node data) {if (Q-> count> = QueueSize) {cout <"overflow" <endl; return;} Q-> Seq [Q-> rear] = data; Q-> rear = (Q-> rear + 1) % QueueSize; Q-> count ++ ;} int QueueNotEmpty (RQueue * Q) {if (Q-> count! = 0) return 1; else return 0;} Node DeleteQueue (RQueue * Q) {if (Q-> count <= 0) {cout <"empty" <endl; exit (0);} Node d; d = Q-> Seq [Q-> front]; Q-> front = (Q-> front + 1) % QueueSize; q-> count --; return d ;} // ======================================/// matrix int mazeArr [4] [4] = {{0, 0, }, {,}; int visit [ROW_COL] [ROW_COL]; // int stepArr [4] [2] = {-}, {}, {0,-1 }}; // perform BFS on it and find the path as the shortest path. Note that the form parameter of the two arrays is int (* mazeArr) [4] Int BFS (int (* mazeArr) [4], Node, int n) {for (int I = 0; I <ROW_COL; I ++) for (int j = 0; j <ROW_COL; j ++) visit [I] [j] = 0; Node N; Initiate_Queue (& Q); AppendQueue (& Q, node); while (QueueNotEmpty (& Q) {N = DeleteQueue (& Q); if (N. x = n-1 & N. y = N-1) {return N. step;} visit [N. x] [N. y] = 1; // traverse the top, bottom, left, and right of the point. If the conditions are met, enter the queue for (int I = 0; I <4; I ++) {int x = N. x + stepArr [I] [0]; int y = N. y + stepArr [I] [1]; if (x> = 0 & y> = 0 & x <n & y <n &&! Visit [x] [y] & mazeArr [x] [y] = 0) {visit [x] [y] = 1; N = fuzhi (x, y, n. step + 1); AppendQueue (& Q, N) ;}} return-1 ;}void main () {Node node; node = fuzhi (, 0 ); cout <BFS (mazeArr, node, ROW_COL); getchar ();}




An example of a C-language wide search (BFS) solution to the Maze problem

Please refer to my document, wenku.baidu.com/...8.html

Maze problem BFS always times out

That's two-way BFS.
Two-way BFS can be improved.
Or your program has a problem .....
Generally, it's okay if BFS passes through the maze 500 ........
Take a good look ~~
I remember that I was never that long ....

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.