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

The program that I\'m writing is to traverse a maze. The maze is an array of cha

ID: 3620064 • Letter: T

Question

The program that I'm writing is to traverse a maze. The maze is an array of characters involving 1's and 0's. The 1's signify walls and 0's signify an open path. I have written methods to read in a file, find the entrance and exit to the maze and generate random mazes. The method that I'm having trouble with is that which would solve the maze and mark the taken path with X's and if the maze is not solvable say so. The method should also print out the solved Maze. The area that I need this bit of code for is "void traverse(void)" My code is given below and any help would be appreciated.

#include
#include
#define M 12
#define N 10
char array[M][N] = {0};
int entranceM, entranceN, exitM, exitN;


main()
{



int rows, columns;

printf("Enter the number of rows "); /*prompt*/
scanf("%d", &rows); /*reads an integer*/
printf("Enter the number of columns "); /*prompt*/
scanf("%d", &columns); /*reads an integer for columns*/
fileread();
findentrance();
printf("EntranceM is %d ", entranceM);
printf("EntranceN is %d ", entranceN);
findexit();
printf("ExitM is %d ", exitM);
printf("ExitN is %d ", exitN);

}

void findentrance(void)/*Finds the entrance of the maze*/
{

int j;
int count = 0;


/*Checks the left side of the maze for an entrance*/
for(j=0; j<=M-1; j++)
{
if(array[j][0] == '0')
{
entranceM = j;
entranceN = 0;
return;
}
}

/*Checks the bottom side of the maze for an entrance*/
for(j=0; j<=N-1; j++)
{
if(array[M-1][j] == '0')
{
entranceM = M-1;
entranceN = j;
return;
}
}

/*Checks the right side of the maze for an entrance*/
for(j=N-1; j>=0; j--)
{
if(array[j][N-1] == '0')
{
entranceM = j;
entranceN = N-1;
return;
}
}

/*Checks the top side of the maze for an entrance*/
for(j-M-1; j>=0; j--)
{
if(array[0][j] == '0')
{
entranceM = 0;
entranceN = j;
return;
}
}


}

void findexit(void)/*Finds the exit of the maze*/
{

int j;
int count = 0;


/*Checks the left side of the maze for an exit*/
for(j=0; j<=M; j++)
{
if(array[j][0] == '0')
{
exitM = j;
exitN = 0;
}
}
/*Checks the bottom side of the maze for an exit*/
for(j=0; j<=N-1; j++)
{
if(array[M-1][j] == '0')
{
exitM = M-1;
exitN = j;
}
}

/*Checks the right side of the maze for an exit*/
for(j=N-1; j>=0; j--)
{
if(array[j][N-1] == '0')
{
exitM = j;
exitN = N-1;
}
}

/*Checks the top side of the maze for an exit*/
for(j=M-1; j>=0; j--)
{
if(array[0][j] == '0')
{
exitM = 0;
exitN = j;
}
}

}


void traverse(void)/*Traverses the maze and marks the traveled path with 'X'*/
{






}



void fileread(void)

{
FILE *fptr;
char c;
char file_name[20];
int i,j;

printf("Type in the name of the file containing the Field ");
scanf("%s",file_name);
fptr=fopen(file_name,"r");
for (i=0; ifor (j=0; jc=fgetc(fptr);
while ( !((c == '1')||(c =='0')) ) c=fgetc(fptr);
array[i][j]=c;
}
fclose(fptr);

for (i=0; ifor (j=0; jif (j == 0) printf(" ");
printf("%c ",array[i][j]);
}
printf(" ");

}

Explanation / Answer

You can write a recursive function to traverse the maze. Start with the entrance point as current position or point. Check if there are any zeros adjacent to the current point if one is found call the function recursively and that point is now made the current one. Check if the current point is the exit. if it is then mark it as X and the points through which you traversed as X. This should give you solution for the maze.