Thread: Help with a maze solver

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    2

    Help with a maze solver

    Hey everyone. I have an assignment that is to use the right hand method to solve a maze. However for some reason my algorithm for solving the maze is stuck at the initial position and is not moving. The first half of my code works, it takes the maze from a file inputs it into an array and then prints it. It can also find the start and end position no problem. once it does all that it gets stuck and keeps outputting that the current position is the start position. any help would be appreciated.
    and also, the walls of the maze are symbolized as a # and the path is just a space.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    You should give us an example maze to work with (why should I have to make one myself?).

    size should be in UPPERCASE (as is the norm for macro names).

    Is the maze SIZE columns and only SIZE - 1 rows? That's what your input/output code seems to say.

    It looks like you could ditch the "flag" variable and simply use the break statement to exit the loop.

    It doesn't look like you're using erow or ecol. They don't seem to be needed.

    You should divide your program into functions.
    Code:
    int main(void) {
        char maze[SIZE][SIZE];
        int srow, scol; 
        read_maze(maze);
        find_start(maze, &srow, &scol);
        solve_maze(maze, srow, scol);
        return 0;
    }
    Your switch cases are missing breaks! And it's very messy and hard to understand. Clearning it up a little still leaves rather incomprehensible code. Do you think a && b == '#' compares both a and b to '#'? It doesn't. If that's what you want, then you need to write a == '#' && b == '#'. Even fixing that, I still can't comprehend what you're trying to do here:
    Code:
        enum { EAST, SOUTH, WEST, NORTH };
    
        switch (dir) 
        { 
        case EAST: 
            if      ((m[x]  [y+1] == '#' && m[x]  [y-1]) == '#' && m[x+1][y]   == ' ')   x++; 
            else if ((m[x]  [y+1] == '#' && m[x+1][y])   == '#' && m[x]  [y-1] == ' ') { y--; dir = NORTH; } 
            else if ((m[x]  [y-1] == '#' && m[x+1][y])   == '#' && m[x]  [y+1] == ' ') { y++; dir = SOUTH; } 
            else if ((m[x]  [y+1] == '#' && m[x]  [y-1]) == ' ' && m[x+1][y]   == '#') { y++; dir = SOUTH; } 
            else if ((m[x+1][y]   == '#' && m[x]  [y+1]) == ' ' && m[x]  [y-1] == '#') { y++; dir = SOUTH; } 
            else if ((m[x]  [y-1] == '#' && m[x+1][y])   == ' ' && m[x]  [y+1] == '#')   x++; 
            else if ((m[x]  [y+1] == '#' && m[x]  [y-1]         && m[x+1][y])  == '#')        dir = WEST; 
            break;
    You also have to make sure you're not accessing outside the bounds of the array.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    2
    Thank you very much Algorism. Yeah I know my coding is very sloppy, i am by no means a programmer. Even through all the mess you still solved my problem for me. I was assuming a&&b== '#' is the same as a=='#' && b=='#'. So thank you again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Claude Shannon's relay circuit maze solver
    By algorism in forum General Discussions
    Replies: 0
    Last Post: 05-22-2016, 04:31 PM
  2. Problems withRecursive Maze Solver
    By FloridaJoe in forum C Programming
    Replies: 6
    Last Post: 10-23-2011, 06:19 PM
  3. maze solver
    By iamnew in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2010, 02:46 AM
  4. Recursive Stack Algorithm Maze Solver
    By unrestricted in forum C Programming
    Replies: 0
    Last Post: 09-04-2007, 03:11 AM
  5. CHeck my Maze Solver
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 07-31-2003, 10:37 PM

Tags for this Thread