Thread: Help on maze problem

  1. #1
    Registered User
    Join Date
    Apr 2010

    Question 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:

    #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.
       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.
       // 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.
       // 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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    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.)

  3. #3
    Registered User
    Join Date
    Sep 2006
    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.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    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...
    fscanf( fp, "%c %d", &rows, &cols );
    fscanf( fp, "%c%d", &startrow, &startcol );
    Basically, if your format was consistent, you could just be doing:
    for( x = 0; fscanf( fp, "%c%d", &row, &col ) == 2 ); x++ )
        switch( x )
            case 0:
                mazerows = row;
                mazecols = col;
            case 1:
                startrow = row;
                startcol = col;
            case 2:
                endrow = row;
                endcol = col;
                ... walls, however you store those ...
        fgetc( fp ); /*strip the one character of whitespace */
    You can get away with removing some of that if you know your file is set up correctly, and make better use of *scanf.

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 01:52 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

Tags for this Thread