如何从迷宫生成二叉树?

时间:2011-11-10 00:09:49

标签: binary-tree binary-search maze

150x150大小的矩阵将描述我们的迷宫,例如,如果矩阵只有10x10,我们会有这样的东西:

   1 1 1 1 1 1 1 1 1 1
   1 0 0 0 0 0 0 1 0 0<-F
   1 0 1 1 0 1 0 1 0 1 
   1 1 1 1 0 1 0 0 0 1
   1 1 1 1 0 1 1 1 1 1
   1 0 0 0 0 1 1 1 1 1
   1 0 1 1 0 1 1 1 1 1
   1 0 1 0 0 0 0 1 1 1
S->0 0 1 1 1 1 0 1 1 1
   1 1 1 1 1 1 1 1 1 1

其中S表示起点,F表示迷宫的出口。 该程序的目的是生成一个二进制树,它将描述我们在尝试查找出口时所经过的所有路径。

你会如何表现出来?我这次真的很失落,我真的不知道从哪里开始这就是为什么我没有发布任何我尝试过的东西但如果你能给我一个方向我真的很感激。

John Smith。

1 个答案:

答案 0 :(得分:1)

您可能想尝试backtracking

这是一个如何解决这个问题的完整例子......但是:它不能在一个带有“岛屿”的迷宫中运行,因为它需要逐步跟踪你已经存在的位置。但我想你也可以弄清楚如何做到这一点......

输出应为:
right, up, up, up, right, right, right, up, up, up, up, right, right, down, down, right, right, up, up, right, finish!

import java.awt.Point;

public class Maze {

    enum Direction {
        up, right, down, left
    }

    static Direction[] dirs = Direction.values();

    int[][] maze = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
            { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, { 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 },
            { 1, 1, 1, 1, 0, 1, 0, 0, 0, 1 }, { 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },
            { 1, 0, 0, 0, 0, 1, 1, 1, 1, 1 }, { 1, 0, 1, 1, 0, 1, 1, 1, 1, 1 },
            { 1, 0, 1, 0, 0, 0, 0, 1, 1, 1 }, { 0, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
    Point start = new Point(0, 8);
    Point finish = new Point(9, 1);

    Point go(Direction dir, Point from) {
        Point result = new Point(from);
        switch (dir) {
        case up:
            if ((from.y == 0) || (maze[from.y - 1][from.x] != 0))
                return null;
            result.translate(0, -1);
            break;
        case right:
            if ((from.x == maze[0].length) || (maze[from.y][from.x + 1] != 0))
                return null;
            result.translate(1, 0);
            break;
        case down:
            if ((from.y == maze.length) || (maze[from.y + 1][from.x] != 0))
                return null;
            result.translate(0, 1);
            break;
        case left:
            if ((from.x == 0) || (maze[from.y][from.x - 1] != 0))
                return null;
            result.translate(-1, 0);
            break;
        }
        return result;
    }

    String tryToGo(Direction dir, Point from) {
        String result;
        Point newPosition = go(dir, from);
        if (newPosition == null)
            return null;
        else if (newPosition.equals(start))
            return null;
        else if (newPosition.equals(finish))
            return "finish!";
        else {
            for (Direction newDir : dirs) {
                switch (newDir) {
                case up:
                    if (dir == Direction.down)
                        continue;
                    break;
                case down:
                    if (dir == Direction.up)
                        continue;
                    break;
                case left:
                    if (dir == Direction.right)
                        continue;
                    break;
                case right:
                    if (dir == Direction.left)
                        continue;
                    break;
                }
                result = tryToGo(newDir, newPosition);
                if (result == null)
                    continue;
                else
                    return newDir + ", " + result;
            }
            return null;
        }
    }

    public static void main(String[] args) {
        Maze maze = new Maze();
        System.out.println("right" + maze.tryToGo(Direction.right, maze.start));

    }
}