I have been working on this maze for many hours, and although this may not be difficult for a seasoned programmer, I am having much difficulty attempting to complete it. I am to find a path through a t by 20 maze and print out the solution in the form: ex. A1 B1 C1 D1 D2 D3 D2 C2...etc. each cell represetingthe correct path through the maze. I will use recursion and so far I have:
Code:
#define west_mask 0x8
#define north_mask 0x4
#define east_mask 0x2
#define south_mask 0x1
int end_cell = "XX"; // the goal
char path[] = ""; // array of cells making up path to end cell.
char walls[]; // array of int showing walls,loaded from file
if (findpath(start_cell, start_cell)
{
printf("Got it. Path is %s", path);
}
else
{
printf("No solution found.");
}
int findpath(this_cell, prev_cell)
{
// Stick this cell onto end of path.
push(path, this_cell);
// WE MADE IT!!! Return success flag.
if (this_cell == end_cell) return 1;
// If no wall to the west, and we didn't come from there.
if !(walls[this_cell] & west_mask) &&
(getcell(this_cell, "west") != prev_cell)
{
if findpath(getcell(this_cell, "west"), this_cell) return 1;
}
// If no wall to the north, and we didn't come from there.
if !(walls[this_cell] & north_mask) &&
(getcell(this_cell, "north") != prev_cell)
{
if findpath(getcell(this_cell, "north"), this_cell) return 1;
}
// If no wall to the east, and we didn't come from there.
if !(walls[this_cell] & east_mask) &&
(getcell(this_cell, "east") != prev_cell)
{
if findpath(getcell(this_cell, "east"), this_cell) return 1;
}
// If no wall to the south, and we didn't come from there.
if !(walls[this_cell] & south_mask) &&
(getcell(this_cell, "south") != prev_cell)
{
if findpath(getcell(this_cell, "south"), this_cell) return 1;
}
// No success from here, so remove this cell from path.
pop(path);
// Return failure flag.
return 0;
}
// Get next cell in some direction.
getcell(current_cell, direction)
{
// Take current_cell and somehow find the next cell
// in the specified direction.
}
push(str_array, value)
{
// Add value to end of str array.
// Or use a real stack.
}
pop(str_array)
{
// Remove last value from str array.
// Or discard last value from a real stack.
I need help reading in the data from a text file that will look like: T 20 A1 P19 14 12 9 ..... where T represents the column length, 20 represents the row length, A! is the starting cell, P19 is the finish cell, and every number after this (i.e. 14 12 9...) represents the sum of the cell walls. Here, south = 1, east = 2, north = 4 and west = 8.