Interview Questions-maze questions-breadth-first search-queue

Source: Internet
Author: User
// Maze problem --- Search for breadth first ----- queue # include <stdio. h ># include <queue >#include <iostream> using namespace STD; # define max_row 5 # define max_col 5 struct point {int row; int Col ;}; queue <point> S; int maze [max_row] [max_col] = {, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}; void print_maze () {for (INT I = 0; I <max_row; ++ I) {for (Int J = 0; j <max_col; ++ J) {printf ("% d", maze [I] [J]);} printf ("\ n ");} printf ("********************* * ** \ N ");} struct point predecessor [max_row] [max_col] ={{-1,-1 },{-1,-1 }, {-1,-1}, {-1,-1}, {-1,-1 }}, {-1,-1}, {-1, -1}, {-1,-1}, {-1,-1}, {-1,-1 }}, {-1,-1 }, {-1,-1}, {-1,-1}, {-1,-1}, {-1,-1 }}, {-1, -1}, {-1,-1}, {-1,-1}, {-1,-1}, {-1,-1 }}, {-1,-1}, {-1,-1}, {-1,-1}, {-1,-1}, {-1, -1 }}; void visit (INT row, int Col, struct point pre) {point visit_point = {row, Col}; maze [row] [col] = 2; predecessor [row] [col] = pre; S. push (visit _ Point) ;}int main () {point P = {0, 0}; maze [p. row] [p. col] = 2; S. push (p); While (! S. empty () {P = S. front (); S. pop (); // arrive at the destination if (P. row = MAX_ROW-1 & P. col = MAX_COL-1) {break;} // move down if (P. row + 1 <max_row & maze [p. row + 1] [p. col] = 0) {visit (P. row + 1, p. col, P);} // move it to the right if (P. col + 1 <max_col & maze [p. row] [p. col + 1] = 0) {visit (P. row, P. col + 1, P);} // move up if (P. col-1> = 0 & maze [p. row] [p. col-1] = 0) {visit (P. row, P. col-1, P);} // move it to the left if (P. row-1> = 0 & maze [p. row-1] [p. col] = 0) {visit (P. row-1, p. col, P);} print _ Maze ();} If (P. row = MAX_ROW-1 & P. col = MAX_COL-1) {printf ("(% d, % d) \ n", p. row, P. COL); While (predecessor [p. row] [p. col]. row! =-1) {P = predecessor [p. row] [p. col]; printf ("(% d, % d) \ n", p. row, P. COL) ;}} else {cout <"no path" <Endl ;}return 0 ;}

 

Running result:

 

2 2 0 0 0
2 0 0 1 0
0 1 0 0 0
0 0 0 0 0
0 0 0 1 0
************************
2 2 0 0 0
2 2 0 1 0
2 1 0 0 0
0 0 0 0 0
0 0 0 1 0
************************
2 2 2 0 0
2 2 0 1 0
2 1 0 0 0
0 0 0 0 0
0 0 0 1 0
************************
2 2 2 0 0
2 2 0 1 0
2 1 0 0 0
2 0 0 0 0
0 0 0 1 0
************************
2 2 2 0 0
2 2 2 1 0
2 1 0 0 0
2 0 0 0 0
0 0 0 1 0
************************
2 2 2 2 0
2 2 2 1 0
2 1 0 0 0
2 0 0 0 0
0 0 0 1 0
************************
2 2 2 2 0
2 2 2 1 0
2 1 0 0 0
2 2 0 0 0
2 0 0 1 0
************************
2 2 2 2 0
2 2 2 1 0
2 1 2 0 0
2 2 0 0 0
2 0 0 1 0
************************
2 2 2 2
2 2 2 1 0
2 1 2 0 0
2 2 0 0 0
2 0 0 1 0
************************
2 2 2 2
2 2 2 1 0
2 1 2 0 0
2 2 0 0 0
2 2 0 1 0
************************
2 2 2 2
2 2 2 1 0
2 1 2 0 0
2 2 2 0 0
2 2 0 1 0
************************
2 2 2 2
2 2 2 1 0
2 1 2 2 0
2 2 2 0 0
2 2 0 1 0
************************
2 2 2 2
2 2 2 1 2
2 1 2 2 0
2 2 2 0 0
2 2 0 1 0
************************
2 2 2 2
2 2 2 1 2
2 1 2 2 0
2 2 2 0 0
2 2 2 1 0
************************
2 2 2 2
2 2 2 1 2
2 1 2 2 0
2 2 2 2 0
2 2 2 1 0
************************
2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2 0
2 2 2 1 0
************************
2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2 0
2 2 2 1 0
************************
2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2 0
2 2 2 1 0
************************
2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2
2 2 2 1 0
************************
2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2
2 2 2 1 0
************************
2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2
2 2 2 1 2
************************
(4, 4)
(3, 4)
(3, 3)
(3, 2)
(3, 1)
(3, 0)
(2, 0)
(1, 0)
(0, 0)
Press any key to continue...

 

 

 

A 0-1 matrix is used to simulate a maze. 1 indicates an obstacle and 0 indicates a channel. 2 indicates the accessed data.

Require entering the maze from the entrance in the upper left corner, and exit the maze from the lower right corner. This article simulates the process of searching for the exit. In the future, I will improve this program so that it can output all feasible paths.

Some questions require that you only go to the right or down. You only need to remove the left and up conditions in this question.

 

 

 

 

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.