Question

Hey everyone, I have a programing projest from teacher, but I don't understand what is says.....Anyone...

Hey everyone, I have a programing projest from teacher, but I don't understand what is says.....Anyone can help?

(Maze Traversal) The following grid of #s and dot ( . ) is a double-subscripted array representation of a maze.

# # # # # # # # # # # #

# .   .   .   # .   .   .   .   .   .   #

.   .   # .   # .   # # # # .   #

# # # .   # .   .   .   .   # .   #

# .   .   .   .   # # # .   # .   .

# # # # .   # .   # .   # .   #

# .   .   # .   # . #   .   # .   #

# # .   # .   # . #   .   # .   #

# .   .   .   .   .   .   .   .   # .   #

# # # # # # .   # # # .   #

# .   .   .   .   .   .   # .   .   .   #

# # # # # # # # # # # #

In the preceding double-subscripted array, the #s represent the walls of the maze and the dots represent squares in the possible paths through the maze. Moves can only be made to a location in the array that contain a dot.

         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 maze turns to the right, you follow the wall to the right. As long as you do not remove your hand from wall, eventually you will arrive at the exit of the maze. There may be a shorter path than the one you have taken, but you are guaranteed to get out of the maze if you follow the algorithm.

         Write recursive function mazeTraverse to walk through the maze. The function should receive as arguments a 12-by-12 character array representing the maze, and the starting location of the maze. As mazeTraverse attempts to locate the exit from the maze, it should place the character X in each square in the path. The function should display the maze after each move so the user can watch as the maze is solved.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>

//main-function
int mazeTraverse(int currentRow, int currentColumn );
void printMaze( const int currentRow, const int currentColumn );

                          // 0    1    2    3    4    5    6    7    8    9    10   11
   char maze[ 12 ][ 12 ] = {{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, // 0
                             {'#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'}, // 1
                             {'.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'}, // 2
                             {'#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'}, // 3
                             {'#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.'}, // 4
                             {'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'}, // 5
                             {'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'}, // 6
                             {'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'}, // 7
                             {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'}, // 8
                             {'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'}, // 9
                             {'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'}, // 10
                             {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} };// 11
char printableMaze[12][12];
int main( void ){
   int startRow = 2, startColumn = 0, state = 0;
   printf("The maze\n********\n");
   printMaze(12, 12 );
   //printf("\n");
    int i,j;
    for(i=0;i<12;i++)
       for(j=0;j<12;j++)
           printableMaze[i][j]=maze[i][j];
   state = mazeTraverse(startRow, startColumn );
  
   if(state == 0){
      printf("\nPlayer found the exit!\n\n");      
   }
   return 0;
}

//global
int startingFlag = 1, direction = 0;

//enumeration
enum Stat { OVER, NOT_OVER };
/*core function*/
int mazeTraverse( int currentRow, int currentColumn ){
   //sub-function
   int gameOver( const int currentRow, const int currentColumn );
   int move( int *currentRow, int *currentColumn, int *currentDirection );
  
   enum Stat State;
  
   //check if game is over
   State = gameOver( currentRow, currentColumn );

   if(State == OVER && startingFlag == 0){
      printMaze( currentRow, currentColumn );
      return OVER; //return OVER indicating succesfully find the exit
      //algorithm for if this function can't find the exit is not available    
   }
  
   //indicate player is ready to move
   if(startingFlag == 1){
      //set first direction based on starting position
      if(currentRow == 0){
         direction = 1;           
      }else if(currentRow == 11){
         direction = 0;   
      }else if(currentColumn == 0){
         direction = 2;   
      }else if(currentColumn == 11){
         direction == 3;   
      }
      startingFlag = 0;          
   }
  
   //seek for next move
   move(&currentRow, &currentColumn, &direction );
   mazeTraverse(currentRow, currentColumn );
   return OVER;
}

/*seek for next move*/
enum Direction { NORTH, SOUTH, EAST, WEST };
int move( int *currentRow, int *currentColumn, int *currentDirection ){
   int posibble[ 4 ] = { 0 };// 1 -> North; 2 -> South; 3 -> East; 4 -> West;
   int counter = 0;
  
   enum Direction Seek;
  
   Seek = *currentDirection;
  
   /*move the player respect to current direction*/
  
   //cover the current position
   maze[ *currentRow ][ *currentColumn ] = '.';
  
   //move the player respect to current direction
   if(Seek == NORTH){
      *currentRow -= 1;
   }else if(Seek == SOUTH){
      *currentRow +=1;   
   }else if(Seek == EAST){
      *currentColumn += 1;
   }else if(Seek == WEST){
      *currentColumn -= 1;
   }
    char te;
   //print each move
   printMaze( *currentRow, *currentColumn );// print maze with player current position
   printf("Press enter to continue.");
   scanf("%c",&te);
   /*analyse for next direction*/
  
   //seek posibble direction
   if(maze[ *currentRow - 1 ][ *currentColumn ] == '.' && Seek != SOUTH){
      posibble[ 0 ] = 1;
      counter++;
   }
   if(maze[ *currentRow + 1 ][ *currentColumn ] == '.' && Seek != NORTH){
      posibble[ 1 ] = 1;
      counter++;
   }
   if(maze[ *currentRow ][ *currentColumn + 1 ] == '.' && Seek != WEST){
      posibble[ 2 ] = 1;
      counter++;
   }
   if(maze[ *currentRow ][ *currentColumn - 1 ] == '.' && Seek != EAST){
      posibble[ 3 ] = 1;
      counter++;
   }
  
   //follow right wall
   //Direction { NORTH, SOUTH, EAST, WEST };
   if(counter == 1){
      if(posibble[ 1 ] == 1){//south
         *currentDirection = 1;
      }else if(posibble[ 2 ] == 1){//east
         *currentDirection = 2;
      }else if(posibble[ 0 ] == 1){//north
         *currentDirection = 0;   
      }else if(posibble[ 3 ] == 1){//west
         *currentDirection = 3;
      }
   }else if(counter == 2){
      if(posibble[ 2 ] == 1 && posibble[ 3 ] == 1){// posibble: EAST, WEST
         if(Seek == SOUTH){
            *currentDirection = 3;
         }else if(Seek == NORTH){
            *currentDirection = 2;   
         }             
      }else if(posibble[ 0 ] == 1 && posibble[ 1 ] == 1){// posibble: NORTH,SOUTH
         if(Seek == EAST){
            *currentDirection = 1;     
         }else if(Seek == WEST){
            *currentDirection = 0;   
         }
      }else if(posibble[ 0 ] == 1 && posibble[ 3 ] == 1){// NORTHWEST
            *currentDirection = 0;     
      }else if(posibble[ 0 ] == 1 && posibble[ 2 ] == 1){// NORTHEAST
            *currentDirection = 2;     
      }else if(posibble[ 1 ] == 1 && posibble[ 2 ] == 1){// SOUTHEAST
            *currentDirection = 1;     
      }else if(posibble[ 1 ] == 1 && posibble[ 3 ] == 1){// SOUTHWEST
            *currentDirection = 3;     
      }
   }else if(counter == 3){
      if(Seek == NORTH){
         *currentDirection = 2;     
      }else if(Seek == SOUTH){
         *currentDirection = 3;   
      }else if(Seek == EAST){
         *currentDirection = 1;   
      }else if(Seek == WEST){
         *currentDirection = 0;   
      }
   }else if(counter == 0){
      //dead end
      if(Seek == NORTH){
         *currentDirection = 1;      
      }else if(Seek == SOUTH){
         *currentDirection = 0;   
      }else if(Seek == EAST){
         *currentDirection = 3;   
      }else if(Seek == WEST){
         *currentDirection = 2;   
      }
   }
  
}

/*check if game is over*/
int gameOver( const int currentRow, const int currentColumn ){
   if(currentRow == 0 || currentRow == 11 || currentColumn == 0 || currentColumn == 11 ){
      return OVER;
   }else{
      return NOT_OVER;   
   }
}

/*print current maze*/
void printMaze( const int currentRow, const int currentColumn ){
    int mazeRow, mazeColumn;
   
    printf("\n    ");
    for(mazeRow = 0; mazeRow < 12; mazeRow++){
      for(mazeColumn = 0; mazeColumn < 12; mazeColumn++){
         if(mazeRow == currentRow && mazeColumn == currentColumn){
            printableMaze[ mazeRow ][ mazeColumn ] = 'X';
         }
         printf("%2c", printableMaze[ mazeRow ][ mazeColumn ] );
        
      }
      printf("\n    ");
   }
   printf("\n");
}

Add a comment
Know the answer?
Add Answer to:
Hey everyone, I have a programing projest from teacher, but I don't understand what is says.....Anyone...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • relevant comments and indentations should be included. Q1: (Recursive Maze Traversal) (75 points) The following grid...

    relevant comments and indentations should be included. Q1: (Recursive Maze Traversal) (75 points) The following grid is a double-subscripted array representation of a maze The # symbols represent the walls of the maze, and the periods (.) represent squares in the possible paths through the maze. There's a simple algorithm for walking through a maze that guarantees finding the exit (assuming there's an exit). If there's not an exit, you'll arrive at the starting location again. Place your right hand...

  • This lab will use 2D arrays, recursive algorithms, and logical thinking. The following grid of hashes(#)...

    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 # # # # # # # # # # # # # . . . # . . . . . . # . . # . # . # # # # . # # # # . # . . . . # . # # . . . . #...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • In this question, you will test, using a backtracking algorithm, if a mouse can escape from...

    In this question, you will test, using a backtracking algorithm, if a mouse can escape from a rectangular maze. To ensure consistency of design, start your solution with maze_start.c. The backtracking algorithm helps the mouse by systematically trying all the routes through the maze until it either finds the escape hatch or exhausts all possible routes (and concludes that the mouse is trapped in the maze). If the backtracking algorithm finds a dead end, it retraces its path until it...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

  • Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two...

    Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two are of type int and are called xLoc and yLoc and the third is of type int called ID. Also add a static variable of type double called scaleFactor. This should be initialized to a default value of 1. Update the constructor to set the three new instance variables and add appropriate get and set methods for the four new variables. All set methods...

  • This is java. Goal is to create a pacman type game. I have most of the...

    This is java. Goal is to create a pacman type game. I have most of the code done just need to add in ghosts score dots etc. Attached is the assignment, and after the assignment is my current code. import java.awt.Color; import java.awt.Dimension; import java.awt.Image; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class Maze extends JFrame implements KeyListener { private static final String[] FILE = {...

  • DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...

    DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide.   A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to...

  • I don't understand what calculations I would have made to create the buffer solution from Part...

    I don't understand what calculations I would have made to create the buffer solution from Part D of the lab. I understood the procedures I took to create the solution but when the report asks to show the calculations used to prepare the buffer solution I am not sure other than mixing the volumes indicated in the procedures section. Thank you pH of Butter Assigned by Instructor 5.12. Measured pH of Assigned B r .9 (Read the procedural Show the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT