# Answer to Question #27745 in C++ for Miranda

Question #27745

I must get to the end of a simple maze using

A various bank of functions, whether is it Booleans, public voids, while, if else.. Etc

Cutting out all numbers is variables

What's the code for the right hand rule, so the robot my follow the right wall- leading it to the finishing point?

A various bank of functions, whether is it Booleans, public voids, while, if else.. Etc

Cutting out all numbers is variables

What's the code for the right hand rule, so the robot my follow the right wall- leading it to the finishing point?

Expert's answer

#define NROWS 3 //you can put here your own values#define MCOLS 3

//for example you have such Maze

//where symbols:

// '.' = open

// 'S' = start

// 'G' = goal

// '+' = path

// 'c' = close

char maze[NROWS][MCOLS] = {

{'c','.','G'},

{'c','c','.'},

{'S','.','.'}

};

//you can use tail recursion to find path with the righthand rule static int steps; //here will be number of steps

int find_path(int x, int y) {

// If x,y isoutside maze, return false.

if ( x < 0|| x > MCOLS - 1 || y < 0 || y > NROWS - 1 )

returntrue;

// If x,y is the goal, return true.

if ( maze[y][x]== 'G') {

returntrue;

}

// If x,y is not open, return false.

if ( maze[y][x]!= '.' && maze[y][x] != 'S' )

return false;

// Mark x,ypart of solution path.

maze[y][x] ='+';

steps++;

// If find_pathNorth of x,y is true, return true.

if (find_path(x, y - 1) == true ) return true;

// If find_pathEast of x,y is true, return true.

if ( find_path(x + 1, y) == true ) returntrue;

// If find_pathSouth of x,y is true, return true.

if (find_path(x, y + 1) == true ) return true;

// If find_pathWest of x,y is true, return true.

if (find_path(x - 1, y) == true ) return true;

//mark therepeat steps

maze[y][x] ='x';

return false;

}

//so simply use this function to check the path in themaze and then display maze with simple cycles

//for example you have such Maze

//where symbols:

// '.' = open

// 'S' = start

// 'G' = goal

// '+' = path

// 'c' = close

char maze[NROWS][MCOLS] = {

{'c','.','G'},

{'c','c','.'},

{'S','.','.'}

};

//you can use tail recursion to find path with the righthand rule static int steps; //here will be number of steps

int find_path(int x, int y) {

// If x,y isoutside maze, return false.

if ( x < 0|| x > MCOLS - 1 || y < 0 || y > NROWS - 1 )

returntrue;

// If x,y is the goal, return true.

if ( maze[y][x]== 'G') {

returntrue;

}

// If x,y is not open, return false.

if ( maze[y][x]!= '.' && maze[y][x] != 'S' )

return false;

// Mark x,ypart of solution path.

maze[y][x] ='+';

steps++;

// If find_pathNorth of x,y is true, return true.

if (find_path(x, y - 1) == true ) return true;

// If find_pathEast of x,y is true, return true.

if ( find_path(x + 1, y) == true ) returntrue;

// If find_pathSouth of x,y is true, return true.

if (find_path(x, y + 1) == true ) return true;

// If find_pathWest of x,y is true, return true.

if (find_path(x - 1, y) == true ) return true;

//mark therepeat steps

maze[y][x] ='x';

return false;

}

//so simply use this function to check the path in themaze and then display maze with simple cycles

## Comments

## Leave a comment