INSTEAD OF READING JUST ONE MAZE I WOULD LIKE THIS PROGRAM TO READ MULTIPLE MAZE
ID: 3650830 • Letter: I
Question
INSTEAD OF READING JUST ONE MAZE I WOULD LIKE THIS PROGRAM TO READ MULTIPLE MAZES FROM TEXT FILE PLEASE HELPTHE MAXIMUM SIZE OF MAZE 50X50
#include <stdio.h>
#define FALSE 0
#define TRUE 1
#define NROWS 6
#define MCOLS 6
// Symbols:
// '.' = open
// '#' = blocked
// 'S' = start
// 'G' = goal
// '+' = path
// 'x' = bad path
// Sample input file:
char maze[NROWS][MCOLS] = {
{'S','#','#','#','#','#'},
{'.','.','.','.','.','#'},
{'#','.','#','#','#','#'},
{'#','.','#','#','#','#'},
{'.','.','.','#','.','G'},
{'#','#','.','.','.','#'}
};
/*
char maze[NROWS][MCOLS] = {
{'S','#','#','#','#','#'},
{'.','.','.','.','.','#'},
{'#','.','#','#','#','#'},
{'#','.','#','#','#','#'},
{'.','.','.','#','#','G'},
{'#','#','.','.','.','#'}
};*/
void
displayMaze(void)
{
int i;
printf("MAZE: ");
for ( i = 0; i < NROWS; i++ )
printf("%.*s ", MCOLS, maze[i]);
printf(" ");
return;
}
/*
FIND-PATH(x, y)
1. if (x,y outside maze) return false
2. if (x,y is goal) return true
3. if (x,y not open) return false
4. mark x,y as part of solution path
5. if (FIND-PATH(North of x,y) == true) return true
6. if (FIND-PATH(East of x,y) == true) return true
7. if (FIND-PATH(South of x,y) == true) return true
8. if (FIND-PATH(West of x,y) == true) return true
9. unmark x,y as part of solution path
10. return false
*/
int findPath(int x, int y)
{
// If x,y is outside maze, return false.
if ( x < 0 || x > MCOLS - 1 || y < 0 || y > NROWS - 1 ) return FALSE;
// If x,y is the goal, return true.
if ( maze[y][x] == 'G' ) return TRUE;
// If x,y is not open, return false.
if ( maze[y][x] != '.' && maze[y][x] != 'S' ) return FALSE;
// Mark x,y part of solution path.
maze[y][x] = '+';
// If find path North of x,y is true, return true.
if ( findPath(x, y - 1) == TRUE ) return TRUE;
// If find path East of x,y is true, return true.
if ( findPath(x + 1, y) == TRUE ) return TRUE;
// If find path South of x,y is true, return true.
if ( findPath(x, y + 1) == TRUE ) return TRUE;
// If find path West of x,y is true, return true.
if ( findPath(x - 1, y) == TRUE ) return TRUE;
// Unmark x,y as part of solution path.
maze[y][x] = 'x';
return FALSE;
}
int
main(void)
{
// Display the input maze
displayMaze();
if ( findPath(0, 0) == TRUE )
printf("Success! ");
else
printf("Failed ");
// Display the output maze
displayMaze();
return 0;
}
INSTEAD OF READING JUST ONE MAZE I WOULD LIKE THIS PROGRAM TO READ MULTIPLE MAZES FROM TEXT FILE PLEASE HELP
Explanation / Answer
please rate - thanks
file has number of rows and columns followed by maze. used this file, same maze twice
#include <stdio.h>
#define FALSE 0
#define TRUE 1
int NROWS,MCOLS;
#define MAX 50
// Symbols:
// '.' = open
// '#' = blocked
// 'S' = start
// 'G' = goal
// '+' = path
// 'x' = bad path
// Sample input file:
char maze[MAX][MAX];
void
displayMaze(void)
{
int i;
printf("MAZE: ");
for ( i = 0; i < NROWS; i++ )
printf("%.*s ", MCOLS, maze[i]);
printf(" ");
return;
}
/*
FIND-PATH(x, y)
1. if (x,y outside maze) return false
2. if (x,y is goal) return true
3. if (x,y not open) return false
4. mark x,y as part of solution path
5. if (FIND-PATH(North of x,y) == true) return true
6. if (FIND-PATH(East of x,y) == true) return true
7. if (FIND-PATH(South of x,y) == true) return true
8. if (FIND-PATH(West of x,y) == true) return true
9. unmark x,y as part of solution path
10. return false
*/
int findPath(int x, int y)
{
// If x,y is outside maze, return false.
if ( x < 0 || x > MCOLS - 1 || y < 0 || y > NROWS - 1 ) return FALSE;
// If x,y is the goal, return true.
if ( maze[y][x] == 'G' ) return TRUE;
// If x,y is not open, return false.
if ( maze[y][x] != '.' && maze[y][x] != 'S' ) return FALSE;
// Mark x,y part of solution path.
maze[y][x] = '+';
// If find path North of x,y is true, return true.
if ( findPath(x, y - 1) == TRUE ) return TRUE;
// If find path East of x,y is true, return true.
if ( findPath(x + 1, y) == TRUE ) return TRUE;
// If find path South of x,y is true, return true.
if ( findPath(x, y + 1) == TRUE ) return TRUE;
// If find path West of x,y is true, return true.
if ( findPath(x - 1, y) == TRUE ) return TRUE;
// Unmark x,y as part of solution path.
maze[y][x] = 'x';
return FALSE;
}
int
main(void)
{FILE *input;
int i,j;
char c;
input = fopen("maze.txt","r");
if(input == NULL)
{ printf("Unable to open input file 1 ");
getch();
return 0;
}
while(fscanf(input,"%d",&NROWS)>0)
{fscanf(input,"%d",&MCOLS);
c=getc(input);
printf("NEW MAZE rows %d cols %d ",NROWS,MCOLS);
i=0;
while(i<NROWS)
{j=0;
while(j<MCOLS)
{c=getc(input);
if(c!=' ');
{maze[i][j]=c;
j++;
}
}
c=getc(input);
i++;
}
// Display the input maze
displayMaze();
if ( findPath(0, 0) == TRUE )
printf("Success! ");
else
printf("Failed ");
// Display the output maze
displayMaze();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.