如何在递归回溯迷宫中修复我的越界异常?

时间:2018-04-28 03:13:27

标签: java maze recursive-backtracking

我的细胞每个都有一个坐标。我的问题是,当我当前的单元格是边框单元格而我随机选择的邻居不在边框内时,我得到一个例外。如何防止此方法在边界外查看?我试图添加x和y,然后检查x和y之和是否小于零然后从switch case中断并通过查找确实存在的相邻单元格来重新开始。

public void generate(Maze mz) {
    Random rand = new Random();

    // generate a x * y grid of cells
    StdDraw.setXscale(0, 10);
    StdDraw.setYscale(0, 10);

    // start at initial cell and push it to the stack
    Coord currentCell = new Coord(0,0);
    stack.push(currentCell);

    int remaining = 1;
    while(remaining <= mz.getCols() * mz.getRows()) {
        // check if currentCell's neighbors have been visited
        loop: if(!stack.contains(currentCell.getNeighbor(Direction.NORTH)) || !stack.contains(currentCell.getNeighbor(Direction.WEST)) || !stack.contains(currentCell.getNeighbor(Direction.SOUTH)) || !stack.contains(currentCell.getNeighbor(Direction.EAST))) {
            // choose a random neighbor
            // TODO: special case when cell is on the border
            int randomDirection = rand.nextInt(4);
            switch(randomDirection) {
            case 0:
                int x0 = currentCell.getNeighbor(Direction.NORTH).getCol();
                int y0 = currentCell.getNeighbor(Direction.NORTH).getRow();
                int sum0 = x0 - y0;
                if(sum0 < 0) {
                    break loop;
                }
                mz.setExit(currentCell, Direction.NORTH, true);
                currentCell = currentCell.getNeighbor(Direction.NORTH);
                stack.push(currentCell);
                remaining++;
                break;
            case 1:
                int x1 = currentCell.getNeighbor(Direction.WEST).getCol();
                int y1 = currentCell.getNeighbor(Direction.WEST).getRow();
                int sum1 = x1 - y1;
                if(sum1 < 0) {
                    break loop;
                }
                mz.setExit(currentCell, Direction.WEST, true);
                currentCell = currentCell.getNeighbor(Direction.WEST);
                stack.push(currentCell);
                remaining++;
                break;
            case 2:
                int x2 = currentCell.getNeighbor(Direction.SOUTH).getCol();
                int y2 = currentCell.getNeighbor(Direction.SOUTH).getRow();
                int sum2 = x2 - y2;
                if(sum2 < 0) {
                    break loop;
                }
                mz.setExit(currentCell, Direction.SOUTH, true);
                currentCell = currentCell.getNeighbor(Direction.SOUTH);
                stack.push(currentCell);
                remaining++;
                break;
            case 3:
                int x3 = currentCell.getNeighbor(Direction.EAST).getCol();
                int y3 = currentCell.getNeighbor(Direction.EAST).getRow();
                int sum3 = x3 - y3;
                if(sum3 < 0) {
                    break loop;
                }
                mz.setExit(currentCell, Direction.EAST, true);
                currentCell = currentCell.getNeighbor(Direction.EAST);
                stack.push(currentCell);
                remaining++;
                break;
            }
        } else {
            currentCell = stack.peek();
            stack.pop();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在考虑边界条件时,该技术是为了防止可能的移动越界。

四个边界条件是:

  • 北:如果你还没有排在第一位,你可以向北移动
  • 南:如果你还不在最底层,你可能会向南移动
  • 东:如果你还没有在最右边,你可能会向东移动
  • West:如果你还没在最左边,你可能会向西移动

转换为代码,它可能看起来像这样:

case 0:  // Want to move North
    // To move north, must be below the top row
    int curRow = currentCell.getRow();
    if(curRow == 0) {   // Assuming top row is assigned to zero
        break loop;
    }

或者,例如:

case 0:  // Want to move North
    // To move north, must be below the top row
    int curRow = currentCell.getRow();
    if(curRow > 0) {   // Assuming top row is assigned to zero
        // Process the move NORTH
    }

SOUTH的案例类似于if (curRow < MAXROW) { .. process ... }

对于EAST / WEST有效移动基于当前列。