Recursion Lab Assignment This lab will use 2D arrays recursi

Recursion Lab Assignment: This lab will use 2D arrays, recursive algorithms, and logical thinking. The following grid of hashes(#) and dots is a 2D array representation of a maze The hashes represent the walls of the maze and the dots represent the possible paths through the maze. Moves can only be made to a location in the array that contains a dot Algorithm Explanation There is a simple algorithm for walking through a maze that GUARANTEES finding the exit (assuming there is an exit). If there is not an exit, you will arrive at the starting location again. Place your right hand on the wall to your right and begin walking forward. Never remove your hand from the wall. If the ma turns to the right, you ze follow the wall to the right. As long as you do not remove your hand from the wall, eventually you will arrive at the exit of the maze. If you follow the algorithm What to do: Write a program to walk through the maze. You must write a recursive function. The function should recieve as arguments 12-by-12 character array representing the maze and the starting point (x, y location in the maze) As the function attempts to escape from the maze it should place an X for the current path taken. The program should display the maze at each step so you can watch as the maze is solved. Put an X where you have traveled and maybe an O where you currently are located. The end of the maze will be a capital F\" (stands for finished) This algorithm doesn\'t work for all mazes. Can you come up with a maze where this doesn\'t work? Rules you must follow: Recursive Algorithm, m use recursive method. Method gets four parameters (maze, xLoc, YLoc, handLocationX, handLocationY) no instance fields needed Your method can only see one step, you can only see what\'s in front of you and what your hand is on..... no teleporting, no looking beyond one step at a time

Solution

Maze Traversal - First of all we outline our program and then w write the required code for it.

Outline of the program
First of all we will declare functions that will be required to traverse the maze, check for the right move, check for the surrounding environment(edges) and the display of the maze.
Then we will store the maze in an array
And last of all we will determine a way out of the maze by using our functions.

// Code to find the exit//

#include <studio.h>
#include <stdlib.h>

#define XSTART 2
#define YSTART 0

// We wil define each direction with the help of an integer value so that we can use them to decide a move//

#define DOWN 0
#define RIGHT 1
#define UP 2
#define LEFT 3

void mazeTraverse(char maze[12][12], int xCor, int yCor, int dir);
int checkForEdge(int x, int y)
int correctMove(char maze[][12], int r, int c);
void displayMaze(char maze[][12]);

int main(void)
{
char maze[12][12] = {
           {\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\'},
           {\'#\',\'-\',\'-\',\'-\',\'#\',\'-\',\'-\',\'-\',\'-\',\'-\',\'-\',\'#\'},
           {\'-\',\'-\',\'#\',\'-\',\'#\',\'-\',\'#\',\'#\',\'#\',\'#\',\'-\',\'#\'},
           {\'#\',\'#\',\'#\',\'-\',\'#\',\'-\',\'-\',\'-\',\'-\',\'#\',\'-\',\'#\'},
           {\'#\',\'-\',\'-\',\'-\',\'-\',\'#\',\'#\',\'#\',\'-\',\'#\',\'-\',\'#\'},
           {\'#\',\'#\',\'#\',\'#\',\'-\',\'#\',\'-\',\'#\',\'-\',\'#\',\'-\',\'#\'},
           {\'#\',\'-\',\'-\',\'#\',\'-\',\'#\',\'-\',\'#\',\'-\',\'#\',\'-\',\'#\'},
           {\'#\',\'#\',\'-\',\'#\',\'-\',\'#\',\'-\',\'#\',\'-\',\'#\',\'-\',\'#\'},
           {\'#\',\'-\',\'-\',\'-\',\'-\',\'-\',\'-\',\'-\',\'-\',\'#\',\'-\',\'#\'},
           {\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'-\',\'#\',\'#\',\'#\',\'-\',\'#\'},
           {\'#\',\'-\',\'-\',\'-\',\'-\',\'-\',\'-\',\'#\',\'-\',\'-\',\'-\',\'#\'},
           {\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\',\'#\'};
              
      
//Call the mazeTraverse function//
mazeTraverse(maze, XSTART, YSTART, RIGHT);
return 0;
}

void mazeTraverse (char maze[12][12], int xCor, int yCOr, int dir)
{
static int flag = 0;
maze[xCor][yCor] = \'X\";
displayMaze (maze);

//Check if maze is complete//
if(checkForEdge (xCor, yCOr) && xCor !- XSTART && yCor !=YSTART)
{
printf(\"Successfully exit the maze\");
return;
}

elseif (xCor == XSTART && yCor == YSTART && flag == 1)
{
printf(\"You are back to the starting position\");
return;
}
else
{
    int counter;
   int nextMove;
   flag = 1;
  
   for (nextMove = dir, counter = 0; counter < 4; ++counter, ++nextMove,    nextMove == 4)
       {
           switch(nextMove)
               {
               case DOWN:
               if(correctMove(maze, xCor + 1, yCor, LEFT);
               return;
               }
               break;

               case RIGHT:
               if(correctMove(maze, xCor, yCor + 1, DOWN);
               return;
               }
               break;
              
               case UP
               if(correctMove(maze, xCor - 1, yCor, RIGHT);
               return;
               }
               break;

               case LEFT
               if(correctMove(maze, xCor, yCor - 1, UP);
               return;
               }
               break;
               }
           }
   }
}

int correctMove(char maze[][12], int r, int c)
{
   return (r>=0 && r<= 11 && c>= 0 && c<= 11 && maze [r][c] != \'#\');
}
int checkForEdge(int x, int y)
{
   if ((x = 0 || x ==11) && (y>= 0 && y<= 11))
   {
       return 1;
   }
   else if (( y == 0 || y == 11) && (x >= 0 && x<= 11))
   {
   return 1;
   }
   else
   {
   return 0;
   }
}
void displayMaze (char maze [][12])
{
   int x;
   int y;
   for (x=0; x < 12; x++)
   {
       for (y =0; y <12; y++)
       {
           printf(\"%c\", maze [x][y]);
       }
   printf(\"\ \");
   }
printf(\"\ Hit enter to go to the next move\");
getchar();
}

 Recursion Lab Assignment: This lab will use 2D arrays, recursive algorithms, and logical thinking. The following grid of hashes(#) and dots is a 2D array repre
 Recursion Lab Assignment: This lab will use 2D arrays, recursive algorithms, and logical thinking. The following grid of hashes(#) and dots is a 2D array repre
 Recursion Lab Assignment: This lab will use 2D arrays, recursive algorithms, and logical thinking. The following grid of hashes(#) and dots is a 2D array repre

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site