Maze Implementation Recursive version C + +
Problem Description:
////////////////////////////////////////////////////////////
Title: Maze Solver problem.
General idea:
1, the entrance, the exit Judgment/procedure terminates the judgment: 4 Azimuth coordinates boundary comparison, indicated to the entrance and exit.
2-1, the solution Principle 1: The violence treatment, starts from the entry point, carries on the feasibility judgment to its four directions, obtains the next position, repeats, knows to walk to the exit.
2-2, the solution Principle 2: For the export of the maze, if you have been on the right, or on the left to walk, must be able to walk to the exit. This program eliminates the 1 violence teams in each direction of discrimination.
3, traversed the route, the specific coordinates of the value of the change to 2, and then will pass the point coordinates into the stack to save.
4, optimize the point ①: There may be a dead end or ring route, then will inevitably detour road. So for 3, the modified value of 2 is improved to +=2, can be specific to go through a location several times
5, optimize the point ②: According to 3, 4, you can judge the way through the unnecessary route, record the value of 4 points, and then stack the stack operation, you can optimize the part of the route.
6, to improve the point: there is no shortest route of the maze solution.
////////////////////////////////////////////////////////////
Map representation in MazeMap.txt under the engineering catalogue
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
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
1 1 0 0 0 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 1 1
1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 1 1
1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1
1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1
1 1 0 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1
1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1
1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1
1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1
1 1 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1 1
1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
code example:
The maze map is read by the file.
voidGetmazemap (int*a,intRowintCol) {FILE*FOut; Fopen_s (&fout,"MazeMap.txt","R"); ASSERT (FOut); for(inti =0; i < row; ++i) { for(intj =0; J <col;) { CharCH =fgetc (FOut); if(ch = ='0'|| ch = ='1') {A[i*col + j] = CH-'0'; ++J; } } }}
Maze PrintingvoidPrintmazemap (int*a,intRowintCol) {cout<<"Mazemap: (row,col):("<< Row <<","<< Col <<")."<<Endl; for(inti =0; i < row; ++i) { for(intj =0; J < Col; ++j) {cout<< A[i*col + j] <<" "; } cout<<Endl; } cout<<Endl;}
The position coordinate class. structstep{intRow//Line intCol//column BOOL operator= = (Step &s) {returnRow = = S.row&&col = =S.col; }};
In the program to traverse the maze, in addition to the coordinates, more specific direction of the class manstructman{Man (Step s,intd): _cur (s), _dir (d) {} Step _cur; int_dir;//0-3 indicates 4 directionsMans Nextpos (intdir) {Step cur=_cur; Dir= (dir+4) %4; Switch(dir) { Case 0: Cur.row--; Break; Case 1: Cur.col++; Break; Case 2: Cur.row++; Break; Case 3: Cur.col--; Break; } returnMan (cur, dir); }};stack<man>paths;
Print the coordinates of a specific maze route (with direction)voidPrintpathstep () { while(!Paths.empty ()) {Man tmp=Paths.top (); cout<<"["<< Tmp._cur.row <<","<< Tmp._cur.col <<"]:"<< Tmp._dir <<""<<" -"; Paths.pop (); } cout<<"over!"<<Endl;}//given map and entry point coordinates, find the maze solutionvoidGetmazepaths (int*map,intRowintCol, step&entry) { //The current position.Man m (entry,0); Map[m._cur.row*col + M._cur.col] =2; Paths.push (m); while(1) {Man top=Paths.top (); Mans cur= Top.nextpos (Top._dir-1); //Man cur = top.nextpos (top._dir + 1); Replaceable upstream, converted to the right row.
if(cur._cur.col<0|| cur._cur.row<0|| Cur._cur.col>=col | | cur._cur.row>=row) {cout<<"Out of bounds"<<Endl; Top._dir++; //top._dir--; Replaceable upstream, converted to right rowPaths.pop (); Paths.push (top); Continue; } //the boundary, i.e. the entrance if(Cur._cur.col = =0|| Cur._cur.row = =0|| Cur._cur.col = = col-1|| Cur._cur.row = = row-1) &&map[cur._cur.row*col + Cur._cur.col] = =0) {cout<<"This is the entrance ."<<Endl; if(! (Cur._cur = =entry)) {Map[cur._cur.row*col + Cur._cur.col] =2; Paths.push (cur); cout<<"Get exported"<<cur._cur.row<<" "<<cur._cur.col<<Endl; Break; } } //Traversal://Next position is the next position in the current direction if(Map[cur._cur.row*col + cur._cur.col]! =1) {Map[cur._cur.row*col + Cur._cur.col] + =2; if(Map[cur._cur.row*col + cur._cur.col] = =4) {Step tmp; Tmp.row=Cur._cur.row; Tmp.col=Cur._cur.col; ////////////////////////////////////////////////////////////////////////////// //Fallback process. while(Paths.top (). _cur.row! = Tmp.row | | paths.top (). _cur.col! =tmp.col) {map[paths.top (). _cur.row*col + paths.top (). _cur.col] =1; Paths.pop (); }} Map[cur._cur.row*col + Cur._cur.col] =2; Paths.push (cur); } Else{Top._dir++; //top._dir--; Replaceable upstream, shift to right-line scenarioPaths.pop (); Paths.push (top); } }}
The problem of maze routing is solved by recursive method. Thinking of the left-line scheme
Note: Recursive mode does not like the non-recursive way to eliminate the dead end of the optimization, but simply left line to get the route. voidGetnextaccesspath (int*map,intRowintCol, man&entry) {Man tmp (entry); if((Entry._cur.row= = Row-1|| Entry._cur.col = = Col-1|| Entry._cur.row = =0 //| | entry._cur.col = = 0//Note, the comment of this line is mainly clear that the specific location of the exit is not the left. It can be improved to conclude that the point is the boundary, but not the entry of the maze into the program. (that is, export)
) && Map[entry._cur.row*col + entry. _cur.col]! =1) {Paths.push (entry); return; } Else{Paths.push (entry); TMP= Entry.nextpos (entry._dir-1);
Get the next place to pass while(Map[tmp._cur.row*col + tmp._cur.col] = =1) {tmp= Entry.nextpos (tmp._dir+1); } entry=tmp; Getnextaccesspath (map, Row, col, entry); }}voidMain () {inta[ -][ -] = {}; :: Getmazemap ((int*) A, -, -); Step ent= {2,0 }; Mans Entm= { {2,0},1 }; Getmazepaths ((int*) A, -, -, ENT); Non-recursive mode //Getnextaccesspath ((int*) A, ten, Ten, Man (ent, 0)); Recursive mode::P Rintmazemap (int*) A, -, -); ::P Rintpathstep (); System ("Pause");}
Run the result instance:
Get exit 2
Mazemap: (row,col):(20,20).
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
2 2 2 2 2 2 2 2 2 2 2 2 2 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1
1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 2 1 1 1 1 0 0 0 1 1 0 1 1 1 1 2 1 1
1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 2 1 1 0 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 1 1 0 1 1 1 1 1 0 1
1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 1 1
1 1 0 1 1 1 2 1 1 1 1 1 1 1 1
1 1 2 2 2 0 2 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1
1 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 2 1 2 2 2 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[19,2]:2--> [18,2]:3--> [17,2]:3--> [16,2]:3--> [15,2]:3--> [14,2]:3--> [14,3]:4--> [14,4]:0-- > [15,4]:1--> [16,4]:1--> [17,4]:1--> [18,4]:5--> [18,5]:4--> [18,6]:4--> [17,6]:3-->
[16,6]:3--> [15,6]:3--> [14,6]:3--> [13,6]:3--> [12,6]:3--> [12,7]:4--> [12,8]:4--> [12,9]:4-- > [12,10]:4--> [12,11]:4--> [12,12]:0--> [12,12]:3--> [12,13]:4--> [12,14]:4--> [12,15]:4-->
[12,16]:4--> [12,17]:4--> [11,17]:3--> [10,17]:3--> [9,17]:3--> [8,17]:3--> [7,17]:3--> [6,17]:3 --[5,17]:3--> [4,17]:3--> [3,17]:3--> [2,17]:3--> [2,16]:2--> [2,15]:2--> [2,14]:2-->
[2,13]:2--> [2,12]:2--> [2,11]:2--> [2,10]:2--> [2,9]:2--> [2,8]:2--> [2,7]:2--> [2,6]:2--> [ 2,5]:2--> [2,4]:2--> [2,3]:2--> [2,2]:2--> [2,1]:2--> [2,0]:2--> Over!
Note: The map size in the program can be defined in the file.
The array representation can then be improved to be dynamically allocated.
Specifically about two-dimensional arrays to do parameters or how to dynamically apply for a two-dimensional array scheme can be consulted: effective-c++.
Maze Implementation Recursive version C + +