Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PROGRAMMING LANGUAGE C You are now allowed to use the following in additional to

ID: 3703081 • Letter: P

Question

PROGRAMMING LANGUAGE C

You are now allowed to use the following in additional to the techniques of the previous chapters:

• pointers

• pointer operators

• pass by reference simulation

• const qualifiers

• size of operator

•function pointers

•character handling library

• string handling library

• string conversion functions

• additional input/output functions from: getchar() , fgets() , putchar() , puts() , sprintf() , sscanf()

W Grades for Valerie x-pMail-moctet X1@replit-Repls 0 (14) which gas eq PROGRAMMING D Homework-08-SF E Stream TV and M ? × ? x x) ? > d | (D file:///C/Users/Valena%20Moctezuma/Downloads/Homework-08-Spring-2018-19620(1).pdf Homework-08-Spring-2018-1 (1).pdf 213 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 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 the wall, eventually you'll arrive O Type here to search 725 PM 4/8/2018

Explanation / Answer

Solution:

code:

#include <stdio.h>

/* This function is an easy way to print the maze. Takes the maze

* as an argument and prints it out. No return value.

/

void print_maze(char maze[12][12]) {

   int i,j;

    for (i = 0; i < 12; ++i) {

        for (j = 0; j < 12; ++j) {

            printf("%c ", maze[i][j]);

        }

        putchar(' ');

    }

    putchar(' ');

}

/* This is the recursive maze traversal function. It takes the maze, a

* beginning cooordinate in the maze, and what direction we are facing at the

* current point in the maze based on our moves, as arguments It first

* evaluates if we are at the ending point, or next to it. Afterward, it

* figures out which way we are facing at the current point in the maze and

* decides how to move based on that, then feeds that info back to itself.

* No return value.

*/

void mazeTraverse(char maze[12][12], int row, int col, char* facing) {

    // For clarity of statemnts, and typing sanity, we'll define directions

    char north = maze[row - 1][col];

    char south = maze[row + 1][col];

    char east = maze[row][col + 1];

    char west = maze[row][col - 1];

    char north_west = maze[row - 1][col - 1];

    char north_east = maze[row - 1][col + 1];

    char south_west = maze[row + 1][col - 1];

    char south_east = maze[row + 1][col + 1];

    // If at the exit

    if (col == 11) {

        printf("Success! ");

    }

    // If the exit is right next to the current position

    else if (col + 1 == 11 && east == '.') {

        maze[row][col + 1] = 'x';

        *facing = '>';

        print_maze(maze);

        mazeTraverse(maze, row, (col + 1), facing);

     }

    // Otherwise, figure out which way we are facing and move accordingly

    else if (*facing == '>') {

       // Move south

       if (

            (south != '#' && south_west == '#' && (north != '.' || north != 'x')) ||

            (south != '#' && south_west != '#' && south_east != '#')

            ) {

            maze[row + 1][col] = 'x';

            *facing = 'v';

            print_maze(maze);

            mazeTraverse(maze, (row + 1), col, facing);

        }

       // Move east

       else if (

            (east != '#' && south_east == '#') ||

            (east != '#' && south_east != '#') ||

           (east != '#' && south_east != '#' && north_east != '#')

            ) {

            maze[row][col + 1] = 'x';

            *facing = '>';

            print_maze(maze);

            mazeTraverse(maze, row, (col + 1), facing);

        }   

        // Move north

        else if (

            (north != '#' && north_east == '#') ||

            (north != '#' && north_east != '#') ||

            (north != '#' && north_east == '#' && north_west != '#')

            ) {

            maze[row - 1][col] = 'x';

            *facing = '^';

            print_maze(maze);

            mazeTraverse(maze, (row - 1), col, facing);

        }

        // Move west

        else if (west != '#' ) {

            maze[row][col - 1] = 'x';

            *facing = '<';

            print_maze(maze);

            mazeTraverse(maze, row, (col - 1), facing);

        }

    }

    else if (*facing == 'v') {

        // Move south

        if (

            (south != '#' && south_west == '#' && (north != '.' || north != 'x')) ||

            (south != '#' && south_west != '#' && south_east != '#') ||

            (south != '#' && south_west != '#' && south_east == '#')

            ) {

            maze[row + 1][col] = 'x';

            *facing = 'v';

            print_maze(maze);

            mazeTraverse(maze, (row + 1), col, facing);

        }

        // Move west

        else if (west != '#' ) {

            maze[row][col - 1] = 'x';

            *facing = '<';

            print_maze(maze);

            mazeTraverse(maze, row, (col - 1), facing);

        }

        // Move east

        else if (

            (east != '#' && south_east == '#') ||

            (east != '#' && south_east != '#') ||

            (east != '#' && south_east != '#' && north_east != '#')

            ) {

            maze[row][col + 1] = 'x';

            *facing = '>';

            print_maze(maze);

            mazeTraverse(maze, row, (col + 1), facing);

        }

        // Move north

        else if (

            (north != '#' && north_east == '#') ||

            (north != '#' && north_east != '#') ||

            (north != '#' && north_east == '#' && north_west != '#')

            ) {

            maze[row - 1][col] = 'x';

            *facing = '^';

            print_maze(maze);

            mazeTraverse(maze, (row - 1), col, facing);

        }

    }

    else if (*facing == '^') {

        // Move east

        if (

            (east != '#' && south_east == '#') ||

            (east != '#' && south_east != '#') ||

            (east != '#' && south_east != '#' && north_east != '#')

            ) {

            maze[row][col + 1] = 'x';

            *facing = '>';

            print_maze(maze);

            mazeTraverse(maze, row, (col + 1), facing);

        }

        // Move north

        else if (

            (north != '#' && north_east == '#') ||

            (north != '#' && north_east != '#') ||

            (north != '#' && north_east == '#' && north_west != '#')

            ) {

            maze[row - 1][col] = 'x';

          *facing = '^';

            print_maze(maze);

            mazeTraverse(maze, (row - 1), col, facing);

        }

        // Move west

        else if (west != '#' ) {

            maze[row][col - 1] = 'x';

            *facing = '<';

            print_maze(maze);

            mazeTraverse(maze, row, (col - 1), facing);

        }

        // Move south

        else if (

            (south != '#' && south_west == '#' && (north != '.' || north != 'x')) ||

            (south != '#' && south_west != '#' && south_east != '#')

            ) {

            maze[row + 1][col] = 'x';

            *facing = 'v';

            print_maze(maze);

            mazeTraverse(maze, (row + 1), col, facing);

        }

    }

    else if (*facing == '<') {

       // Move west

        if (

            (west != '#' && north != '.')

            ) {

            maze[row][col - 1] = 'x';

            *facing = '<';

            print_maze(maze);

            mazeTraverse(maze, row, (col - 1), facing);

        }

        // Move north

        else if (

            (north != '#' && north_east == '#') ||

            (north != '#' && north_east != '#') ||

            (north != '#' && north_east == '#' && north_west != '#')

            ) {

            maze[row - 1][col] = 'x';

            *facing = '^';

            print_maze(maze);

            mazeTraverse(maze, (row - 1), col, facing);

        }

        // Move east

        else if (

            (east != '#' && south_east == '#') ||

            (east != '#' && south_east != '#') ||

            (east != '#' && south_east != '#' && north_east != '#')

            ) {

            maze[row][col + 1] = 'x';

            *facing = '>';

            print_maze(maze);

            mazeTraverse(maze, row, (col + 1), facing);

        }

        // Move south

        else if (

            (south != '#' && south_west == '#' && (north != '.' || north != 'x')) ||

            (south != '#' && south_west != '#' && south_east != '#')

            ) {

            maze[row + 1][col] = 'x';

            *facing = 'v';

            print_maze(maze);

            mazeTraverse(maze, (row + 1), col, facing);

        }

    }

}

int main(void) {

    char maze[12][12] = {'#','#','#','#','#','#','#','#','#','#','#','#',

                        '#','.','.','.','#','.','.','.','.','.','.','#',

                        '.','.','#','.','#','.','#','#','#','#','.','#',

                        '#','#','#','.','#','.','.','.','.','#','.','#',

                        '#','.','.','.','.','#','#','#','.','#','.','.',

                        '#','#','#','#','.','#','.','#','.','#','.','#',

                        '#','.','.','#','.','#','.','#','.','#','.','#',

                        '#','#','.','#','.','#','.','#','.','#','.','#',

                        '#','.','.','.','.','.','.','.','.','#','.','#',

                        '#','#','#','#','#','#','.','#','#','#','.','#',

                        '#','.','.','.','.','.','.','#','.','.','.','#',

                        '#','#','#','#','#','#','#','#','#','#','#','#'};

    int row,i;

    int col = 0;

    /* Decide with direciton we're facing. This will be '^', '<',

     * '>', or 'v', so as not to conflict with the cardinal

     * directions. We'll initialize it to '>' since we know

     * the maze starts out facing east.

    */

    char facing = '>';

    // Find starting point

    for (i = 0; i < 12; ++i) {

        if (maze[i][0] == '.') {

            row = i;

            break;

        }

    }

    // Put an 'x' in the starting point

    maze[row][col] = 'x';

    // Run the maze traversal

    mazeTraverse(maze, row, col, &facing);

    return 0;

}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote