繁体   English   中英

EmptyStackException - 二维数组上的 Java 深度优先搜索算法

[英]EmptyStackException - Java Depth First Search algorithm on 2D array

我在这里查看了这个问题我尝试了那里的大部分代码示例,但是当我在代码中使用它时,它只是跳过了算法。

我有这个使用堆栈的 DFS 算法,我得到一个EmptyStackException ,我已经调试了算法,在第一次递归搜索后堆栈为空,第一次搜索有效,但堆栈的大小设置为 0,我在这里错过了什么? github

如何确保第一次搜索后堆栈不为空?

我得到异常的那一行是while(true){AddBridges state = gameTree.peek(); ... while(true){AddBridges state = gameTree.peek(); ...

我正在使用 2d 数组从 0 到 4 随机生成节点0 = null 1-4 = island每次用户开始游戏时,该数组都会生成随机整数,这可能会导致算法停止运行,

经过一个周末的调试,我发现算法有时会在搜索 4-6 次后停止,有时会在第一次搜索后停止。

public int[][] debug_board_state_easy = new int[4][4];

//This Generates random 2d array
private void InitializeEasy() {
   Random rand = new Random();

   setCurrentState(new State(WIDTH_EASY));
   for (int row = 0; row < debug_board_state_easy.length; row++) {
      for (int column = 0; column < debug_board_state_easy[row].length; column++) {
          debug_board_state_easy[row][column] = Integer.valueOf(rand.nextInt(5));
      }
  }

  for (int row = 0; row < debug_board_state_easy.length; row++) {
     for (int column = 0; column < debug_board_state_easy[row].length; column++) {
        System.out.print(debug_board_state_easy[row][column] + " ");
     }
     System.out.println(debug_board_state_easy);
  }
//I am applying the search algorithm here...
  this.search();

  for (int row = 0; row < WIDTH_EASY; ++row) {
     for (int column = 0; column < WIDTH_EASY; ++column) {
         getCurrentState().board_elements[row][column] = new BoardElement();
         getCurrentState().board_elements[row][column].max_connecting_bridges = Integer.valueOf(debug_board_state_easy[row][column]);
              getCurrentState().board_elements[row][column].row = row;
              getCurrentState().board_elements[row][column].col = column;

         if (getCurrentState().board_elements[row][column].max_connecting_bridges > 0) {
            getCurrentState().board_elements[row][column].is_island = true;
         }
      }
   }
}

void search() {
   Map<Point, List<Direction>> remainingOptions = new HashMap<>();
   Stack<Land> gameTree = new Stack<>();
   gameTree.push(new AddBridges(debug_board_state_easy));

   while(true) {
      AddBridges state = gameTree.peek();
      int[] p = state.lowestTodo();
      if (p == null)
         System.out.println("solution found");
      // move to next game state
         int row = p[0];
         int column = p[1];
         System.out.println("expanding game state for node at (" + row + ", " + column + ")");

         List<Direction> ds = null;
         if(remainingOptions.containsKey(new Point(row,column)))
            ds = remainingOptions.get(new Point(row,column));
         else{
            ds = new ArrayList<>();
            for(Direction dir : Direction.values()) {
               int[] tmp = state.nextIsland(row, column, dir);
               if(tmp == null)
                  continue;
               if(state.canBuildBridge(row,column,tmp[0], tmp[1]))
                  ds.add(dir);
            }
            remainingOptions.put(new Point(row,column), ds);
         }
      // if the node can no longer be expanded, and backtracking is not possible we quit
         if(ds.isEmpty() && gameTree.isEmpty()){
            System.out.println("no valid configuration found");
            return;
         }
      // if the node can no longer be expanded, we need to backtrack
         if(ds.isEmpty()){
            gameTree.pop();
            remainingOptions.remove(new Point(row,column));
            System.out.println("going back to previous decision");
            continue;
         }

         Direction dir = ds.remove(0);
         System.out.println("connecting " + dir.name());
         remainingOptions.put(new Point(row,column), ds);

         AddBridgesnextState = new AddBridges(state);
         int[] tmp = state.nextIsland(row,column,dir);
         nextState.connect(row,column, tmp[0], tmp[1]);
         gameTree.push(nextState);
      }
   }
}

添加桥梁类

public class AddBridges {

    private int[][] BRIDGES_TO_BUILD;

    private boolean[][] IS_ISLAND;
    private Direction[][] BRIDGES_ALREADY_BUILT;

    public Land(int[][] bridgesToDo){
        BRIDGES_TO_BUILD = copy(bridgesToDo);

        int numberRows = bridgesToDo.length;
        int numberColumns = bridgesToDo[0].length;
        BRIDGES_ALREADY_BUILT = new Direction[numberRows][numberColumns];
        IS_ISLAND = new boolean[numberRows][numberColumns];
        for(int i=0;i<numberRows;i++) {
            for (int j = 0; j < numberColumns; j++) {
                BRIDGES_ALREADY_BUILT[i][j] = null;
                IS_ISLAND[i][j] = bridgesToDo[i][j] > 0;
            }
        }
    }

    public AddBridges (AddBridges other){
        BRIDGES_TO_BUILD = copy(other.BRIDGES_TO_BUILD);
        int numberRows = BRIDGES_TO_BUILD.length;
        int numberColumns =  BRIDGES_TO_BUILD[0].length;
        BRIDGES_ALREADY_BUILT = new Direction[numberRows][numberColumns];
        IS_ISLAND = new boolean[numberRows][numberColumns];
        for(int i=0;i<numberRows;i++) {
            for (int j = 0; j < numberColumns; j++) {
                BRIDGES_ALREADY_BUILT[i][j] = other.BRIDGES_ALREADY_BUILT[i][j];
                IS_ISLAND[i][j] = other.IS_ISLAND[i][j];
            }
        }
    }

    public int[] next(int r, int c, Direction dir){
        int numberRows = BRIDGES_TO_BUILD.length;
        int numberColumns = BRIDGES_TO_BUILD[0].length;

        // out of bounds
        if(r < 0 || r >=numberRows || c < 0 || c >= numberColumns)
            return null;


        // motion vectors
        int[][] motionVector = {{-1, 0},{0,1},{1,0},{0,-1}};
        int i = Arrays.asList(Direction.values()).indexOf(dir);

        // calculate next
        int[] out = new int[]{r + motionVector[i][0], c + motionVector[i][1]};

        r = out[0];
        c = out[1];

        // out of bounds
        if(r < 0 || r >=numberRows || c < 0 || c >= numberColumns)
            return null;

        // return
        return out;
    }

    public int[] nextIsland(int row, int column, Direction dir){
        int[] tmp = next(row,column,dir);
        if(tmp == null)
            return null;
        while(!IS_ISLAND[tmp[0]][tmp[1]]){
            tmp = next(tmp[0], tmp[1], dir);
            if(tmp == null)
                return null;
        }
        return tmp;
    }

    public boolean canBuildBridge(int row0, int column0, int row1, int column1){
        if(row0 == row1 && column0 > column1){
            return canBuildBridge(row0, column1, row1, column0);
        }
        if(column0 == column1 && row0 > row1){
            return canBuildBridge(row1, column0, row0, column1);
        }
        if(row0 == row1){
            int[] tmp = nextIsland(row0, column0, Direction.EAST);
            if(tmp == null)
                return false;
            if(tmp[0] != row1 || tmp[1] != column1)
                return false;
            if(BRIDGES_TO_BUILD[row0][column0] == 0)
                return false;
            if(BRIDGES_TO_BUILD[row1][column1] == 0)
                return false;
            for (int i = column0; i <= column1 ; i++) {
                if(IS_ISLAND[row0][i])
                    continue;
                if(BRIDGES_ALREADY_BUILT[row0][i] == Direction.NORTH)
                    return false;
            }
        }
        if(column0 == column1){
            int[] tmp = nextIsland(row0, column0, Direction.SOUTH);
            if(tmp == null)
                return false;
            if(tmp[0] != row1 || tmp[1] != column1)
                return false;
            if(BRIDGES_TO_BUILD[row0][column0] == 0 || BRIDGES_TO_BUILD[row1][column1] == 0)
                return false;
            for (int i = row0; i <= row1 ; i++) {
                if(IS_ISLAND[i][column0])
                    continue;
                if(BRIDGES_ALREADY_BUILT[i][column0] == Direction.EAST)
                    return false;
            }
        }
        // default
        return true;
    }

    public int[] lowestTodo(){
        int R = BRIDGES_TO_BUILD.length;
        int C = BRIDGES_TO_BUILD[0].length;

        int[] out = {0, 0};
        for (int i=0;i<R;i++) {
            for (int j = 0; j < C; j++) {
                if(BRIDGES_TO_BUILD[i][j] == 0)
                    continue;
                if (BRIDGES_TO_BUILD[out[0]][out[1]] == 0)
                    out = new int[]{i, j};
                if (BRIDGES_TO_BUILD[i][j] < BRIDGES_TO_BUILD[out[0]][out[1]])
                    out = new int[]{i, j};
            }
        }
        if (BRIDGES_TO_BUILD[out[0]][out[1]] == 0) {
            return null;
        }
        return out;
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    private int[][] copy(int[][] other){
        int[][] out = new int[other.length][other.length == 0 ? 0 : other[0].length];
        for(int r=0;r<other.length;r++)
            out[r] = Arrays.copyOf(other[r], other[r].length);
        return out;
    }

    public void connect(int r0, int c0, int r1, int c1){
        if(r0 == r1 && c0 > c1){
            connect(r0, c1, r1, c0);
            return;
        }
        if(c0 == c1 && r0 > r1){
            connect(r1, c0, r0, c1);
            return;
        }
        if(!canBuildBridge(r0, c0, r1, c1))
            return;

        BRIDGES_TO_BUILD[r0][c0]--;
        BRIDGES_TO_BUILD[r1][c1]--;

        if(r0 == r1){
            for (int i = c0; i <= c1 ; i++) {
                if(IS_ISLAND[r0][i])
                    continue;
                BRIDGES_ALREADY_BUILT[r0][i] = Direction.EAST;
            }
        }
        if(c0 == c1){
            for (int i = r0; i <= r1 ; i++) {
                if(IS_ISLAND[i][c0])
                    continue;
                BRIDGES_ALREADY_BUILT[i][c0] = Direction.NORTH;
            }
        }
    }
}

你的问题的一部分对我来说是问题的根源:“我在这里错过了什么”? 单元测试,除非我只是没有在你的项目中看到它们。

诸如“每次用户开始游戏时数组都会生成随机整数,这是否会导致算法停止运行?”之类的问题是单元测试存在的原因,以及以下内容:

  1. 在对最终不会成为问题的代码部分编写测试的过程中,您将明确证明它们不是问题。
  2. 如果您正在使用的代码无法按原样进行测试,或者“太复杂”而无法测试,重新编写它将使您成为更好的设计师,并且通常会导致“我不敢相信我没有”没有看到”的时刻。
  3. 当您重构这个程序(降低复杂性,重命名变量以使其更易于理解等)时,如果出现问题,您将立即收到通知,而您不必再花一个周末来试图弄清楚它。

例如,与其在搜索它的方法中随机化棋盘,不如在其他地方随机化它,然后将其作为参数放入该方法(或放入类的构造函数中)。 这样,您可以使用您自己提供的值初始化您自己的测试板,并查看某些板是否比其他板工作得更好以及为什么。 将较大的方法拆分为较小的方法,每个方法都有自己的参数和测试。 目标是用较小的已确认工作的部分制作一个程序,而不是制作一个巨大的东西,然后想知道问题是你认为的小部分还是其他东西。

从长远来看,您将节省大量时间和挫折,并且最终会领先于那些编写数小时然后调试数小时的人。

代码中有很多内容,但我注意到的第一件事可能会有所帮助:

// if the node can no longer be expanded, we need to backtrack
            if(ds.isEmpty()){
                gameTree.pop();
                remainingOptions.remove(new Point(row,column));
                System.out.println("going back to previous decision");
                continue;
            }

您从堆栈中弹出,并继续下一个 while(true) 迭代,此时堆栈中可能没有任何内容,因为您还没有添加任何其他内容。

我同意@Rosa -

删除或查找空堆栈时应发生 EmptyStackException -

======代码中的迭代/状态======

 **if(ds.isEmpty()){**  //HashMap isEmpty = true and gameTree.size() = 1
            gameTree.pop();    // After removing element gameTree.size() = 0 (no elements in stack to peek or pop)
            remainingOptions.remove(new Point(row,column));
            System.out.println("going back to previous decision");
            continue;  //Skip all the instruction below this, continue to next iteration
        }

========下一次迭代========

  while(true){
            AddBridges state = gameTree.peek(); // gameTree.size() = 0 and a peek 

操作将失败,程序将返回 EmptyStackException。

必需的 isEmpty 检查 -

if(gameTree.isEmpty()){ 
            System.out.println("no valid configuration found");
            return;
        }
        AddBridges state = gameTree.peek(); 

因为,函数没有执行任何操作,而是 while 循环。 如果要处理其他指令,则需要“中断”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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