Java - 不能突破这种递归方法

时间:2017-11-19 21:36:14

标签: java recursion return

我正在尝试实现depht first search alogrithm(我的代码可能很糟糕,我很抱歉)。现在我想让它成为一种递归方法,但是一旦满足结束条件,我似乎无法突破它。您在方法中看到的第一个if条件应该突破该方法。当我调试项目时,它到达了return语句,然后立即跳转到方法的末尾。但是不是停止整个事情,而是回到while(!allNeighboursVisited)循环并继续进行无限循环。

我试图通过自己解决这个问题但是没有工作并开始在网上搜索,但我找不到解决问题的方法。

编辑:决定在我的github上分享项目链接,供大家试用:https://github.com/Equiphract/Maze

编辑2:更新了代码;我把它砍在一起所以请不要期待任何令人愉快的事情:)

这是递归方法:

public void depthFirstSearch(int x, int y, Tile[][] maze) {
    // Return method after every Tile is visited.
    if (this.visitedCounter == maze.length * maze[0].length) {
        this.stack.clear();
        return;
    }

    Tile currentTile = maze[x][y];
    Random r = new Random();
    int neighbourAmount = currentTile.getNeighbourAmount();
    boolean allNeighboursVisited = false;
    int stopCounter = 0;

    // If it is a new Tile, mark it as visited
    if (!currentTile.isVisited()) {
        currentTile.setVisited(true);
        this.visitedCounter++;
        stack.add(currentTile);
    }

    // Check if neighbours are not yet visited and "visit" one of them.
    while (!allNeighboursVisited) {
        int random;
        do {
            random = r.nextInt(neighbourAmount);
        } while (this.excludeList.contains(random));

        Tile neighbour = currentTile.getNeighbours().get(random);
        if (!neighbour.isVisited()) {
            if (neighbour.getX() == currentTile.getX() - 1) {
                currentTile.getWall(4).setOpen(true);
                neighbour.getWall(2).setOpen(true);
            } else if (neighbour.getX() == currentTile.getX() + 1) {
                currentTile.getWall(2).setOpen(true);
                neighbour.getWall(4).setOpen(true);
            } else if (neighbour.getY() == currentTile.getY() - 1) {
                currentTile.getWall(1).setOpen(true);
                neighbour.getWall(3).setOpen(true);
            } else if (neighbour.getY() == currentTile.getY() + 1) {
                currentTile.getWall(3).setOpen(true);
                neighbour.getWall(1).setOpen(true);
            }
            this.excludeList.clear();
            depthFirstSearch(neighbour.getX(), neighbour.getY(), maze);
            if (this.visitedCounter == maze.length * maze[0].length) {
                this.stack.clear();
                return;
            }
        } else {
            this.excludeList.add(random);
            stopCounter++;
        }

        if (stopCounter == neighbourAmount) {
            allNeighboursVisited = true;
        }
    }

    // If every neighbour has already been visited, go back one Tile.
    if (!this.stack.isEmpty()) {
        this.stack.remove(this.stack.size() - 1);
        if (!this.stack.isEmpty()) {
            Tile backtrackTile = this.stack.get(this.stack.size() - 1);
            this.excludeList.clear();
            depthFirstSearch(backtrackTile.getX(), backtrackTile.getY(), maze);
            if (this.visitedCounter == maze.length * 3) {
                this.stack.clear();
                return;
            }
        }
        this.excludeList.clear();
    }
}

你知道吗,这里是Tile-Object(很抱歉在这短时间内进行了大量的编辑):

public class Tile {
    private ArrayList<Wall> walls;
    private ArrayList<Tile> neighbours;
    private int x;
    private int y;
    private boolean visited;

    /*
     * Constructor of the Tile class.
     */
    public Tile(int x, int y) {
        this.walls = new ArrayList<Wall>();
        this.neighbours = new ArrayList<Tile>();

        this.walls.add(new Wall(1));
        this.walls.add(new Wall(2));
        this.walls.add(new Wall(3));
        this.walls.add(new Wall(4));

        this.x = x;
        this.y = y;
        this.visited = false;
    }

    /*
     * Returns the ArrayList walls.
     */
    public ArrayList<Wall> getWalls() {
        return walls;
    }

    /*
     * Returns the value of visited.
     */
    public boolean isVisited() {
        return visited;
    }

    /*
     * Sets the value of visited to a specified value.
     * 
     * @param visited a boolean value
     */
    public void setVisited(boolean visited) {
        this.visited = visited;
    }

    /*
     * Returns a wall with the specified position.
     * 
     * @param position the position of the wall
     */
    public Wall getWall(int position) {
        for(Wall w : this.walls) {
            if(w.getPosition() == position) {
                return w;
            }
        }
        return null;
    }

    public int getNeighbourAmount() {
        return this.neighbours.size();
    }

    public ArrayList<Tile> getNeighbours(){
        return this.neighbours;
    }


    /*
     * Adds a Tile to the ArrayList neighbours-
     * 
     * @param t a Tile
     */
    public void addNeighbour(Tile t) {
        this.neighbours.add(t);
    }

    /**
     * @return the x
     */
    public int getX() {
        return x;
    }

    /**
     * @return the y
     */
    public int getY() {
        return y;
    }
}

1 个答案:

答案 0 :(得分:0)

好的我觉得我找到了解决问题的方法。它远非完美,需要大量的优化,也许你们其中一个人想要这样做并在这里发布^^。

我的主要错误是在每次递归调用方法后都没有添加返回,这导致了无限循环。

这是我的解决方案:

public void depthFirstSearch(int x, int y, Tile[][] maze) {
    // Return method after every Tile is visited.
    if (this.visitedCounter == maze.length * maze[0].length) {
        this.stack.clear();
        return;
    }

    Tile currentTile = maze[x][y];
    Random r = new Random();
    int neighbourAmount = currentTile.getNeighbourAmount();
    boolean allNeighboursVisited = false;
    int stopCounter = 0;

    // If it is a new Tile, mark it as visited
    if (!currentTile.isVisited()) {
        currentTile.setVisited(true);
        this.visitedCounter++;
        stack.add(currentTile);
    }

    // Check if neighbours are not yet visited and "visit" one of them.
    while (!allNeighboursVisited) {
        int random;
        do {
            random = r.nextInt(neighbourAmount);
        } while (this.excludeList.contains(random));

        Tile neighbour = currentTile.getNeighbours().get(random);
        if (!neighbour.isVisited()) {
            if (neighbour.getX() == currentTile.getX() - 1) {
                currentTile.getWall(4).setOpen(true);
                neighbour.getWall(2).setOpen(true);
            } else if (neighbour.getX() == currentTile.getX() + 1) {
                currentTile.getWall(2).setOpen(true);
                neighbour.getWall(4).setOpen(true);
            } else if (neighbour.getY() == currentTile.getY() - 1) {
                currentTile.getWall(1).setOpen(true);
                neighbour.getWall(3).setOpen(true);
            } else if (neighbour.getY() == currentTile.getY() + 1) {
                currentTile.getWall(3).setOpen(true);
                neighbour.getWall(1).setOpen(true);
            }
            this.excludeList.clear();
            depthFirstSearch(neighbour.getX(), neighbour.getY(), maze);
            return;
        } else {
            this.excludeList.add(random);
            stopCounter++;
        }

        if (stopCounter == neighbourAmount) {
            allNeighboursVisited = true;
        }
    }

    // If every neighbour has already been visited, go back one Tile.
    if (!this.stack.isEmpty()) {
        this.stack.remove(this.stack.size() - 1);
        if (!this.stack.isEmpty()) {
            Tile backtrackTile = this.stack.get(this.stack.size() - 1);
            this.excludeList.clear();
            depthFirstSearch(backtrackTile.getX(), backtrackTile.getY(), maze);
            return;
        }
        this.excludeList.clear();
    }
}