Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code provided :- #include stdio.h #define SIZE 10 //#define DEBUG // GLOBAL DATA // a cell type containing a row and column position // and

image text in transcribed

Code provided :-

#include "stdio.h"

#define SIZE 10

//#define DEBUG

// GLOBAL DATA

// a cell type containing a row and column position

// and the compass direction most recently moved

struct cell_type {

int row;

int col;

int dir; // the most recent compass direction, 'n', 's', 'e', 'w', 0

};

typedef struct cell_type Cell;

Cell sol[SIZE*SIZE]; // the solution represented as an array of Cells

int maze[SIZE][SIZE] = { // the maze

1,1,1,1,1,1,1,1,1,1,

1,0,0,0,1,1,0,0,0,1,

1,0,1,0,0,1,0,1,0,1,

1,0,1,1,0,0,0,1,0,1,

1,0,0,1,1,1,1,0,0,1,

1,1,0,0,0,0,1,0,1,1,

1,0,0,1,1,0,0,0,0,1,

1,0,1,0,0,0,0,1,1,1,

1,0,0,0,1,0,0,0,0,1,

1,1,1,1,1,1,1,1,1,1

};

// FUNCTION PROTOTYPES

void build(int);

void printSolution(int);

int cellOk(int);

int getNextCell(int);

void main(void)

{

// set starting position and direction

sol[0].row = 1;

sol[0].col = 1;

sol[0].dir = 0;

// start recursive solution

build(0);

}

/*

A recursive function to determine a path through the maze.

Called for each cell in the solution array. Finds the next

valid cell to move to, then calls itself for the next cell.

Inside the function, all possible moves for the current cell

are tried before the function returns.

*/

void build(int n)

{

// *** YOU WRITE THIS FUNCTION ***

// ...

// a handy debug function

#ifdef DEBUG

printf("Iteration: %d At: (%d, %d) Trying: (%d, %d) ",

n, sol[n].row, sol[n].col, sol[n + 1].row, sol[n + 1].col);

#endif

// ...

}

/*

Outputs the current solution array.

*/

void printSolution(int n)

{

int i;

printf(" A solution was found at: ");

for (i = 0; i

printf("(%d, %d) ", sol[i].row, sol[i].col);

printf(" ");

}

/*

Determines the next cel to try. Increments the position

of the next cell and increments the direction of current cell.

Directions are tried in the order east, south, west, north.

*/

int getNextCell(int n)

{

// set initial position and direction for the next cell

sol[n + 1].row = sol[n].row;

sol[n + 1].col = sol[n].col;

sol[n + 1].dir = 0;

// try all positions; east, south, west, north

// increment direction of current cell

// increment postion of next cell

switch (sol[n].dir) {

case 0:

sol[n].dir = 'e';

sol[n + 1].col++;

return 1;

case 'e':

sol[n].dir = 's';

sol[n + 1].row++;

return 1;

case 's':

sol[n].dir = 'w';

sol[n + 1].col--;

return 1;

case 'w':

sol[n].dir = 'n';

sol[n + 1].row--;

return 1;

case 'n':

return 0; // all directions have been tried

}

return 0; // make compiler happy

}

/*

Checks if a cell is a valid move. Invalid moves are cells

that are blocked with a wall, or with a cell that is

part or the present path.

*/

int cellOk(int n)

{

int i;

// check if cell is a border cell

if (maze[sol[n + 1].row][sol[n + 1].col])

return 0;

// check if we are attempting to cross our own path

for (i = 0; i

// if where we want to go is somewhere we've been...

if (sol[n + 1].row == sol[i].row && sol[n + 1].col ==

sol[i].col)

return 0;

return 1;

}

Recursive Maze Algorithm Implementation Write a program to find all solutions to the maze shown. use hard-coded array initialization to define the maze. The output should be a character string of the form Your program may A solution was found at: (3, 8) (4, 8) (4, 7) (5, 7) (6, 7) (6, 6) (7, 6) (8, 6) (8, 7) (8, 8) A solution was found at: 7) (6, 7) (6, 6) (7, 6) (7, 5) (8, 5) (8, 6) (8, 7) (8, 8) etc...for all possible solutions Resources There is a Visual Basic executable version you may want to use to check your an C++ code for this lab is available in the class notes except for the recursive algorithm details which you have to write swers. All the Requirements 1) You MUST base your solution on the supporting code structure and functions provided language if you wish) (Although you may translate it into another lan 2) You MUST use a recursive build algorithm similar to that in the N-Queens problem. (Also posted) 3) You MUST use the maze txt data provided in the demos for submission of final solutions 4) Your program must find and output ALL the TEXT solutions to the maze Turn in: 1) Complete paper source code listings 2) Paper output captures of ALL SOLUTIONS AS TEXT STRINGS OF ORDERED PAIRS Documentation standards: Follow the requirements in the syllabus regarding assignment submission format. Recursive Maze Algorithm Implementation Write a program to find all solutions to the maze shown. use hard-coded array initialization to define the maze. The output should be a character string of the form Your program may A solution was found at: (3, 8) (4, 8) (4, 7) (5, 7) (6, 7) (6, 6) (7, 6) (8, 6) (8, 7) (8, 8) A solution was found at: 7) (6, 7) (6, 6) (7, 6) (7, 5) (8, 5) (8, 6) (8, 7) (8, 8) etc...for all possible solutions Resources There is a Visual Basic executable version you may want to use to check your an C++ code for this lab is available in the class notes except for the recursive algorithm details which you have to write swers. All the Requirements 1) You MUST base your solution on the supporting code structure and functions provided language if you wish) (Although you may translate it into another lan 2) You MUST use a recursive build algorithm similar to that in the N-Queens problem. (Also posted) 3) You MUST use the maze txt data provided in the demos for submission of final solutions 4) Your program must find and output ALL the TEXT solutions to the maze Turn in: 1) Complete paper source code listings 2) Paper output captures of ALL SOLUTIONS AS TEXT STRINGS OF ORDERED PAIRS Documentation standards: Follow the requirements in the syllabus regarding assignment submission format

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Administration The Essential Reference

Authors: Brian Laskey, David Kreines

1st Edition

1565925165, 978-1565925168

More Books

Students also viewed these Databases questions

Question

friendliness and sincerity;

Answered: 1 week ago