递归迷宫求解方法

时间:2012-12-08 14:07:30

标签: java algorithm recursion maze

我正在研究一种递归求解单元格的方法。

该方法完全不起作用。任何建议,将不胜感激。

  

参数:srow =起始x值。 scol =凝视y值erow =结束   x值。 ecol =结束y值。 L =已解决路径点的链接列表

代码:

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;
            }
        }
    }



}

2 个答案:

答案 0 :(得分:0)

这是一个基本的图遍历问题。我建议你使用dfs而不是bfs。几乎所有关于算法和数据结构的教科书都有实现。

您只需调整递归部分即可在达到目标后停止搜索。另一方面,如果您正在寻找目标的所有路径,只需执行所有路径,然后从那里开始。有关提示,您可以查看Bellman-Ford或Dijkstra的算法(http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm)。再一次,任何有关图表章节的好教科书。

答案 1 :(得分:0)

来自Another similar thread,只是为了用一种不那么冗长的语言来看待问题,请看一下 这个由用户@Sergey Rudenko制作的小型JS递归迷宫求解器

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

是的。它的小巧,简单,天真和缺乏但是heck,它的递归和它的工作原理! 此外,您可以清楚地看到许多迷宫遍历算法的常见部分。

对于更严肃的阅读,this page有很多关于许多游戏相关算法的优秀深入但非科学论文教程。

购买算法时需要回答一些相关问题:

需要解决方案吗?

需要每个解决方案吗?

需要最快吗?

迷宫的地形是什么?一个网格?图表?

希望将来实施移动成本吗?

想要实施启发式选择最佳路线?

最后,如果您还没有遇到它,请检查*算法。很受欢迎。

玩得开心!

相关问题