This time we explore the maze of small games.
Let's look at how we can get a path that is implemented using stacks.
When it is the path, the node presses the stack. When the end is not reached, out of the stack, looking for intersections, looking for access.
650) this.width=650; "title=" qq20160412184806.jpg "src=" http://s4.51cto.com/wyfs02/M01/7E/EA/wKioL1cM6vbS8_ X4aacqdstuu5k851.jpg "alt=" Wkiol1cm6vbs8_x4aacqdstuu5k851.jpg "/>
Like this in the first line to store the specifications of the maze (here for a few, define a square maze), design a maze, the maze in a. txt format in the directory (can be anywhere, following the default path for example).
Assuming the entrance is (2,0), the exit is the last line of the maze anywhere.
MAZE.h
#pragma once#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>using namespace std; #include <assert.h> #include <stack>class pos //the coordinates of each point of the maze {public: pos (int row,int col) :_row (Row) , _col (col) {} int _row; int _col;}; Void printpos (pos& pos)// print node coordinates { cout << "(" << pos._ row << ", " << pos._col << ") ";} Int* getmaze (int& n)//open maze from file { file *font = fopen ("Maze.txt", "R"); assert (font != null);//Cannot open maze file no meaning char ch; while ((Ch = fgetc ( Font) != ' \ n ') { N = N * 10 + ch - ' 0 '; } int *a = new int[N*N]; for (int i = 0; i < N*N, (ch = fgetc (font)) != EOF;) { if (ch == ' 1 ' | | ch == ' 0 ') { a[i] = ch - ' 0 '; i++; } } return a;} Void printmaze (int *a, const int n)//print maze { cout << "\ n Maze map (' 0 ' for the road, ' 1 ' for the Wall) " << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << a[i * 10 + j] << " "; } cout << endl; }}bool isoverscope (Pos pos, &NBSP;CONST&NBSP;INT&NBSP;N)//To determine whether to cross { if (pos._col < 0 | | pos._col >= n | | pos._row < 0 | | pos._row >= n) &NBSP;{&NBSP;&NBsp;return true; } return false;} Bool isendpoint (Pos pos, const int n) //judgment is the end point: Set the end of the maze to be able to reach the maze N-1 row { if (Pos._col >= 0 && pos._col < n && pos._row == n - 1) { return true; } return false;} Bool searchmazepath (int* a, const int n, pos enrty, stack<pos> & paths) //search pathway { //If a position node is 0, the compression stack, and the data is changed to 2, looking for this node up and down the position of 0 nodes, and then the pressure stack, //if there is no position up or down for 0 nodes, The Stack assert (a) is searched for the nodes on the upper and lower left and right of the previous node for 0 (a); pos top = paths.top (); a[top._row*n + top._col] = 2; while (! Isoverscope (Paths.top (), n))//each time to determine whether the coordinates cross-border { if (0 == a[(top._ row - 1) *n + top._col])//Upper { a[(top._row - 1) *N + top._col] = 2; pos tmp (Top._row - 1, top._col); paths.push (TMP); top = paths.top (); continue; } if (0 == &NBSP;A[TOP._ROW&NBSP;*&NBSP;N&NBSP;+&NBSP;TOP._COL&NBSP;+&NBSP;1])//Right { a[top._ Row * n + top._col + 1] = 2; pos tmp (Top._row, top._col + 1); paths.push (TMP); top = paths.top (); continue; } if (0 == a[(top._row + 1) *n + top._col])//Lower { a[(top._row + 1) *n + top._col] = 2; pos tmp (Top._row + 1, top._col); paths.push (tmp ); top = paths.top (); continue; } if (0 == a[TOP._ROW&NBSP;*&NBSP;N&NBSP;+&NBSP;TOP._COL&NBSP;-&NBSP;1])//left { a[top._row * n + top._col - 1] = 2; pos tmp (top._row, top._ col - 1); paths.push (TMP); top = paths.top (); continue; } //if (0 == a[top._row * n + TOP._COL&NBSP;+&NBSP;1]&NBSP;&&&NBSP;TOP._COL&NBSP;+&NBSP;1&NBSP;<&NBSP;N)//Right //{ // a[top._row * N + top._col + 1] = 2; // Pos tmp (top._row, top._col + 1); // paths.push (TMP); // Top = paths.top (); // continue; //} //determine whether to reach the exit if (Isendpoint (top, n)) { return true; } //fallback if (A[top._row*n + top._col] == 2 && !paths.empty ()) { paths.pop (); if (!paths.empty ()) { top = paths.top (); continue; } else { return false; } } } //if (Isoverscope (top, n) | | paths.empty ())//from top to left return false; } void printpath (stack<Pos> paths) //print path { //At least paths has one element enrty at the bottom assert (!paths.empty ()); cout < < "Access: " << endl;; while (!paths.empty ()) { printpos (Paths.top ()); paths.pop (); } cout << endl;}
Test.cpp
#include "MAZE.h" void Test () {//Assume that the labyrinth is n*n square int N = 0; int *a = Getmaze (N); Printmaze (A, N); Pos Enrty (2,0); stack<pos> paths; Paths.push (Enrty); if (Searchmazepath ((int*) A, n, Enrty, Paths)) {Printmaze (A, n); Printpath (paths); } else {Printmaze (A, N); cout << "There is not a path in this maze!" << Endl; }}int Main () {test (); System ("pause"); return 0;}
Let's take a look at the results of the operation.
650) this.width=650; "title=" qq20160412200923.jpg "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M01/7E/EA/ Wkiol1cm7tqh7kdtaagbzqputmc656.jpg "alt=" Wkiol1cm7tqh7kdtaagbzqputmc656.jpg "/>
Try changing the last line's ' 0 ' to 1, making it a maze without a path.
650) this.width=650; "title=" qq20160412200951.jpg "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M02/7E/EA/ Wkiol1cm7tratqz5aafofp2vahu424.jpg "alt=" Wkiol1cm7tratqz5aafofp2vahu424.jpg "/>
We can think about:
When there are several pathways, can we get the shortest circuit?
650) this.width=650; "title=" qq20160412204805.jpg "src=" http://s4.51cto.com/wyfs02/M00/7E/EB/ Wkiol1cm73xho3-laaggwygzpsq566.jpg "alt=" Wkiol1cm73xho3-laaggwygzpsq566.jpg "/>
We can get the following ideas:
Record the minimum number of steps, turn the exit to 1 when you reach the exit, look for the next exit, and update the shortest path.
To look for this shortest path, you can look for it once, and output the path when the number of steps in the path is consistent with the shortest number of steps.
But there is a big problem with the above approach: Although a result can be obtained, it is not guaranteed to be the shortest.
Because, when looking for a pathway in the logic "upper right and left" in the order of the above programming, it is possible to block another shorter path, which can affect the results of the shortest path.
650) this.width=650; "title=" qq20160412202007.jpg "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M00/7E/EA/ Wkiol1cm7tvd2xugaadpbskb9oc161.jpg "alt=" Wkiol1cm7tvd2xugaadpbskb9oc161.jpg "/>
So, what do we do? Looking forward to the next blog post.
Finding the Maze path problem