### maze

anyone interested in explaining how to Write a C++ program that solves recursively the Maze Problem
by steps:

A robot is asked to navigate a maze. It is placed at a certain position (the starting position) in
the maze and is asked to try to reach another position (the goal position). Positions in the
maze will either be open or blocked with an obstacle. Positions are identified by (x,y)
coordinates.
At any given moment, the robot can only move 1 step in one of 4 directions. Valid moves are:
• Go North: (x,y) -> (x,y-1)
• Go East: (x,y) -> (x+1,y)
• Go South: (x,y) -> (x,y+1)
• Go West: (x,y) -> (x-1,y)
Note that positions are specified in zero-based coordinates (i.e., 0...size-1, where size is the
size of the maze in the corresponding dimension). The robot can only move to positions
without obstacles and must stay within the maze. The robot should search for a path from the
starting position to the goal position (a solution path) until it finds one or until it exhausts all
possibilities. In addition, it should mark the path it finds (if any) in the maze.
Representation
To make this problem more concrete, let's consider a maze represented by a matrix of
characters. An example
6x6 input maze is:
S # # # # # '.' - where the robot can move (open positions)
. . . . . # '#' - obstacles (blocked positions)
# . # # # # 'S' - start position (here, x=0, y=0)
# . # # # # 'G' - goal (here, x=5, y=4)
. . . # . G
# # . . . #
Aside: Remember that we are using x and y coordinates (that start at 0) for maze positions. A
y coordinate therefore corresponds to a row in the matrix and an x coordinate corresponds to
a column.
A path in the maze can be marked by the '+' symbol...
A path refers to either a partial path, marked while the robot is still searching:
+ # # # # #
+ + + + . #
# . # # # #
# . # # # #
. . . # . G
# # . . . #
You can use the A* (A Star) search algorithm:
http://en.wikipedia.org/wiki/A*_search_algorithm

If the robot only moves one space at a time, then you would need to add each position to an array of found entries. If the robot has not found the goal, and it has already traversed that position, then it should not go that way again. There is always the left hand rule to solving mazes, which is following the left wall around will eventually get you out. Its not very fast, but it is consistent, and works every time.

So if you are ever trapped in a maze, like Jack Nicholson was, and you need to find a way out, follow the left wall.
times up for my assignment question.
but i found this code on the internet can anyone explain for me just for my own knowledge and my curiosity?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245`` ``````#include #include #include #include using namespace std; class Maze{ private: char maze[6][6]; int moves; string history[200]; int locationx; int locationy; public: Maze(){ moves=0; history[0]="Start"; srand((unsigned)time(0)); for (int i=0;i<6;i++){ for(int x=0;x<6;x++){ int random; for(int index=0; index<1; index++){ random= (rand()%3)+1; //Change the 3 to a bigger number for an easier maze } if (random==1){ maze[i][x]='S'; } else{ maze[i][x]='G'; } } } for (int i=0;i<6;i++){ maze [0][i]='S'; maze [i][0]='S'; maze [i][5]='S'; maze [5][i]='S'; } locationx=1; locationy=1; } void printmaze(){ for (int i=0;i<6;i++){ cout<
Topic archived. No new replies allowed.