Help on maze problem
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:
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.
#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);
printf("No solution found.");
int findpath(this_cell, prev_cell)
// Stick this cell onto end of path.
// 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.
// Return failure flag.
// Get next cell in some direction.
// Take current_cell and somehow find the next cell
// in the specified direction.
// Add value to end of str array.
// Or use a real stack.
// Remove last value from str array.
// Or discard last value from a real stack.
I assume you mean that the columns go from A through T.
You will need to read in the size of the maze, then make an array. You appear to be "flattening" your maze into one-dimension, so you'll need to figure out how to represent going east, south, west, and north inside that one-dimensional representation. (You'll also need to figure out how to get from cell 75 to cell D15 to print things out.)
Aren't you going to need some little niceties like, oh I don't know - int main(), some include files, and some code showing what you're trying to do.
It's misleading to post "I'm having trouble with" <something>, when you don't have a single line of code to try and do that <something>.
Well, OK, I guess you are having trouble, but the trouble is, you just haven't tried yet. :p
You just need to pay attention to what it is you're reading, and read them in the order you expect. I wonder why you use numbers for columns. It would also be better if you were consistent with your input. (T SPACE 20 vs ANOSPACE1) But anyway...
Basically, if your format was consistent, you could just be doing:
fscanf( fp, "%c %d", &rows, &cols );
fscanf( fp, "%c%d", &startrow, &startcol );
You can get away with removing some of that if you know your file is set up correctly, and make better use of *scanf.
for( x = 0; fscanf( fp, "%c%d", &row, &col ) == 2 ); x++ )
switch( x )
mazerows = row;
mazecols = col;
startrow = row;
startcol = col;
endrow = row;
endcol = col;
... walls, however you store those ...
fgetc( fp ); /*strip the one character of whitespace */