1. Skip this problem if you solved it for lab 11. Complete Project 5 in Chapter
ID: 3707312 • Letter: 1
Question
1. Skip this problem if you solved it for lab 11. Complete Project 5 in Chapter 10 on page 563. Write a function that accepts an 8 by 8 array of characters that represents a maze. Each position can contain either an X or a blank. Starting at position (0, 1), list any path through the maze to get to location (7, 7). Only horizontal and vertical moves are allowed. If no path exists, write a message indicating there is no path. Moves can be made only to locations that contain a blank. If an X is encountered that path is blocked and another must be chosen. Use recursion. 2. Write a function which returns the sum of the digits of an integer value. You must use recursion. Example sum (765) 18, sum (16978) 31, etc Write a function which recursively reverses a string. 4. Tower of Hanoi: A very popular mathematical game or puzzle is referred to as the Tower of Hanoi. The idea behind the game is to find an efficient method for moving disks between three posts. Each disk has a different diameter, but all of them can be placed on the available posts. The goal of the game is to move all of the disks from one post to another according the following rules: 1. Only one disk may be transferred at a time 2. Only the top disk on any post may be accessed at a given time 3. No disk may be placed on top of a smaller disk at any point At the start of the game, all of the disks must originally be placed such that the largest disk is on the bottom of the stack of one post, and the smallest is on the top of the stack on the same post. The disks should form a cone shape. Write a program to simulate the Tower of Hanoi game. For each move print the post number (1 3) from which the disk is taken, the diameter of the disk, and the resulting post on which the disk is placed. Also, show the current diameter of the disks on each post. You must use recursion to solve this problem! Initially start with three disks in your game. Note: if you visit http://towersofhanoi.info/Animate.aspx, you will find an animation of how Tower of Hanoi should run.Explanation / Answer
Solution:
Note: The first question is done as per Chegg guidelines, please repost others.
code:
#include<stdio.h>
#define size 8 //Size of the maze
bool mazeSolver(char maze[size][size], int solution[size][size],int i, int j)
{
if(i == size-1 && j == size-1)
{
solution[i][j] = 1;
return true;
}
if(i >= 0 && i < size && j >= 0 && j < size && maze[i][j] == 'O') //if maze[x][y] is a valid cell of maze i.e not x, y not representing any cell out of maze and maze[x][y] is blank i.e a move can be made
{
solution[i][j] = 1; // add maze[i][j] to the solution
if (mazeSolver(maze, solution, i+1, j))
return true;
if (mazeSolver(maze, solution, i, j+1))
return true;
solution[i][j] = 0;
return false;
}
return false;
}
bool init_mazeSolver(char maze[size][size])
{
int solution[size][size] = { {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
if(mazeSolver(maze, solution, 0, 0) == false)
{
printf("sizeo path exist");
return false;
}
printf(" Maze steps: ");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
if(solution[i][j] == 1)
printf(" (%d, %d) ", i,j);
}
return 0;
return true;
}
int main()
{
char maze[size][size] = {
{'O','O','X','X','X','X','X','X'},
{'X','O','X','X','X','X','X','X'},
{'X','O','O','O','O','X','X','X'},
{'X','X','X','X','O','X','X','X'},
{'X','X','X','X','O','O','O','X'},
{'X','X','X','X','X','X','O','X'},
{'X','X','X','X','X','O','O','O'},
{'X','X','X','X','X','X','X','O'}
};
printf(" Given Maze... ");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
printf("%c ", maze[i][j]);
printf(" ");
}
init_mazeSolver(maze);
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. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.