Maze File: maze1.txt Current position: (0,13) Current position: (1,13) Current position: (2,13) Current position: (3,13) Current position: (3,12) Current position: (3,11) Current position: (3,10) Current position: (3,9) Current position: (3,8) Current position: (3,7) Current position: (4,7) Current position: (5,7) Current position: (5,6) Current position: (5,5) Current position: (5,4) Current position: (5,3) Current position: (6,3) Current position: (7,3) Current position: (7,4) Current position: (7,5) Current position: (7,6) Current position: (7,7) Current position: (7,8) Current position: (7,9) Current position: (8,9) Current position: (9,9) Current position: (9,10) Current position: (9,11) Current position: (9,12) Current position: (9,13) Current position: (10,13) Current position: (11,13) Current position: (12,13) Current position: (13,13) Current position: (13,14) Current position: (13,15) Current position: (14,15) Current position: (15,15) Current position: (16,15) total steps:206 directory steps:39 Maze solved
Maze File: maze2.txt Current position: (0,20) Current position: (1,20) Current position: (1,19) Current position: (2,19) Current position: (3,19) Current position: (3,20) Current position: (3,21) Current position: (4,21) Current position: (5,21) Current position: (5,20) Current position: (5,19) Current position: (6,19) Current position: (7,19) Current position: (7,18) Current position: (7,17) Current position: (7,16) Current position: (7,15) Current position: (8,15) Current position: (9,15) Current position: (10,15) Current position: (11,15) Current position: (11,14) Current position: (11,13) Current position: (12,13) Current position: (13,13) Current position: (13,12) Current position: (13,11) Current position: (12,11) Current position: (11,11) Current position: (11,10) Current position: (11,9) Current position: (12,9) Current position: (13,9) Current position: (13,8) Current position: (13,7) Current position: (13,6) Current position: (13,5) Current position: (13,4) Current position: (13,3) Current position: (13,2) Current position: (13,1) Current position: (14,1) total steps:260 directory steps:42 Maze solved ----------------------------------------------------------------------------------------------------- Maze File: maze3.txt (不水了 total steps:392 directory steps:175 Maze solved
综上,完成了三个部分的要求
代码如下
$maze.h$
/**************************************************** * Functions to solve mazes. * * * * Datafile must still contain size as first data. * * * * Four functions are only stubs. * ****************************************************/
#include<iostream> #include<fstream> usingnamespace std; #ifndef MAZE_H #define MAZE_H // The maze itself is indicated by # for the walls // All other locations in the maze can be any other character // Global variables defining the maze to be solved
// These functions provide access to the maze // as well as provide manipulation of direction // of motion and maze location // See implementation for details structPosition { int H, V; }; int mazeWidth, mazeHeight; char *maze; int *posi; int *post; int i=0; enumDirection {DOWN, LEFT, UP, RIGHT}; voidFindEntrance(int&); boolAtExit(int); voidReportPosition(int); voidWheresRight(int,Direction,int&); boolWall(int); voidTurnRight(Direction&); voidMoveForward(int&,Direction); voidWheresAhead(int,Direction,int&); voidTurnLeft(Direction&);
// This function loads the maze from the specified file // returning the maze and its dimensions // The height of the maze is not actually used anywhere but here //mazeh,w分别是x,y。i设置全局,用于为路径数组定位记录 boolLoadMaze(constchar fname[]);
// This function solves the maze using the 'hand on left wall' // rule, printing the maze position as it proceeds
voidSolveMaze(); // This function scans the maze array for the first non-wall item // It assumes that the entrance is in the top row of the maze array voidFindEntrance(int& pos); // This function returns true if the maze position is the exit // identified by being in the last row of the array
boolAtExit(int pos); // This function displays the position in the maze // At this time it specifies row and column of the array