简体   繁体   中英

Recursive Maze Solving Method

I am working on a method to recursively solve made up of cells.

The method just quite isn't working. Any suggestions would be appreciated.

Parameters: srow = starting x value. scol = staring y value erow = end x value. ecol = end y value. L = Linked List of solved path points

Code:

private InputGraphicMaze2 maze;
private int R, C; 

//code added by me
private String[] [] cell; //an array to keep track of cells that are proven dead ends. 


public YourMazeWithPath2() 
{       
   // an R rows x C columns maze
  maze = new InputGraphicMaze2();

  R=maze.Rows(); C=maze.Cols();  

  //code added by me
  cell = new String[R+2][C+2];
  for (int i=0; i<R+2; i++) {
      for (int k=0; k<C+2; k++) {
          cell[i][k] = "no";
      }
  }

  // Path holds the cells of the path
  LinkedList<Point> Path = new LinkedList<Point>();
   // Create the path
   CreatePath(maze, 1, 1, R, C, Path);
   // show the path in the maze
   maze.showPath(Path);
}

private void setDead(int x, int y) {
    cell[x][y] = "dead";
}

private void setVisited(int x, int y) {
    cell[x][y] = "visited";
}

public boolean CreatePath(InputGraphicMaze2 maze,      
  int srow, int scol, int erow, int ecol, LinkedList<Point> L)
{

    int x = srow;
    int y = scol;
    Point p = new Point(x, y);

    if ((x<1) || (y<1) || (x>R) || (y>C)) {
        return false; //cell is out of bounds
    }

    else if ((x==R) && (y==C)) {
        return true; // cell is the exit cell
    }

    else {
        if ((maze.can_go(x, y, 'U')) && (x!=1) && (!cell[x-1][y].equals("dead")) && (!cell[x-1][y].equals("visited"))) {
            L.addLast(p);
            setVisited(x,y);
            CreatePath(maze, x-1, y, R, C, L);
            return false;
        }
        else if ((maze.can_go(x, y, 'R')) && (y!=C) && (!cell[x][y+1].equals("dead")) && (!cell[x][y+1].equals("visited"))) {
            L.addLast(p);
            setVisited(x, y);
            CreatePath(maze, x, y+1, R, C, L);
            return false;
        }
        else if ((maze.can_go(x, y, 'D')) && (x!=R) && (!cell[x+1][y].equals("dead")) && (!cell[x+1][y].equals("visited"))) {
            L.addLast(p);
            setVisited(x, y);
            CreatePath(maze, x+1, y, R, C, L);
            return false;
        }
        else if ((maze.can_go(x, y, 'L')) && (y!=1) && (!cell[x][y-1].equals("dead")) && (!cell[x][y-1].equals("visited"))) {
            L.addLast(p);
            setVisited(x, y);
            CreatePath(maze, x, y-1, R, C, L);
            return false;
        }
        else {
            if ((maze.can_go(x, y, 'U')) && (x!=1) && (cell[x][y-1].equals("visited"))) {
                setDead(x, y);
                if (L.contains(p))
                    L.remove(p);
                CreatePath(maze, x-1, y, R, C, L);
                return false;
            }
            else if ((maze.can_go(x, y, 'R')) && (y!=C) && (cell[x][y+1].equals("visited"))) {
                setDead(x, y);
                if (L.contains(p))
                    L.remove(p);
                CreatePath(maze, x, y+1, R, C, L);
                return false;
            }
            else if ((maze.can_go(x, y, 'D')) && (x!=R) && (cell[x+1][y].equals("visited"))) {
                setDead(x, y);
                if (L.contains(p))
                    L.remove(p);
                CreatePath(maze, x+1, y, R, C, L);
                return false;
            }
            else if ((maze.can_go(x, y, 'D')) && (y!=1) && (cell[x][y-1].equals("visited"))) {
                setDead(x, y);
                if (L.contains(p))
                    L.remove(p);
                CreatePath(maze, x, y-1, R, C, L);
                return false;
            }
            else {
                return false;
            }
        }
    }



}

This is a basic graph-traversal problem. I suggest you use dfs as opposed to bfs. Pretty much any textbook on algorithms and datastructure will have the implementation.

You simply have to tweak the recursive part to stop searching once you have reached the goal. On the other hand, if you are looking for all paths to the goal, just do all-to-all path and then go from there. For hints, you can look up Bellman–Ford or Dijkstra's algorithm (http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm). Again, any good textbook with a chapter on graphs.

From Another similar thread , just for seeing the problem in a less verbose language, take a look at this tiny JS recursive maze solver made by user @Sergey Rudenko

var map = [
    [1,1,0,0,0,0,0,0],
    [0,1,1,0,0,0,0,0],
    [1,1,1,0,0,0,0,0],
    [1,0,0,1,1,1,1,1],
    [1,1,0,0,1,0,0,1],
    [0,1,1,0,1,0,0,1],
    [1,1,1,0,1,0,0,1],
    [1,0,0,0,1,1,1,1]
]

var goalx = 7;
var goaly = 7;

function findpath(x,y){

    // illegal move check
    if (x < 0 || x > (map[0].length -1) || y < 0 || y > (map.length - 1)) return false; //if it is outside of map
    if (map[y][x]==0) return false; //it is not open

    // end move check
    if (x== goalx && y== goaly){
        console.log('Reached goal at: ' + x + ':' + y);
        return true; // if it is the goal (exit point)
    }

    if(map[y][x] == 9 || map[y][x] == 8)
        return false;

    console.log('Im here at: ' + x + ':' + y);

    map[y][x]=9; //here marking x,y position as part of solution path outlined by "9"

    if(findpath(x+1,y)) 
        return true;    
    if(findpath(x,y+1)) 
        return true;    
    if(findpath(x,y-1))
        return true;
    if(findpath(x-1,y)) 
        return true;                    

    return false;
};

findpath(0, 0);

JSfiddle

Yep. Its tiny, simplistic, naive and lacking but heck, its recursive and it works! Besides, you see clearly common parts to many maze traversing algorithm.

For a more serious reading, this page has excellent in-depth-but-not-scientific-paper tutorials about many game related algorithms.

There are some pertinent questions to answer when shopping for an algorithm:

Need any solution?

Need every solution?

Need the fastest?

Whats the topography of the maze? A grid? A graph?

Want to implement movement cost in the future?

Want to implement heuristics to choose best route?

Finally, if you didn't come across it yet, check a* algorithm. Very popular.

Have fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM