1

我正在尝试制作一个使用递归执行八皇后的程序,但我不断收到数组越界错误。我已经有一段时间遇到这个问题了,我似乎无法确定问题所在。这是我的代码:

public class Queens {

 public int currColumn = 0;
 public static final int BOARD_SIZE = 8;
 public static final int EMPTY = 0;
 public static final int QUEEN = 1;
 private int board[][];

 public Queens() {
  board = new int[BOARD_SIZE][BOARD_SIZE];
 }

 public void clearBoard() {
  for (int x = 0; x <= BOARD_SIZE; x++) {
   for (int y = 0; y <= BOARD_SIZE; y++) {
    board[x][y] = 0;
   }

  }
 }

 public void displayBoard() {
  for (int x = 0; x < BOARD_SIZE; x++) {
   System.out.print("\n");
   for (int y = 0; y < BOARD_SIZE; y++) {
    System.out.print(board[x][y]);
   }

  }
 }

 public boolean placeQueens(int column) {
  if (column > BOARD_SIZE) {
   return true;
  } else {
   boolean queenPlaced = false;
   int row = 1;

   while (!queenPlaced && (row <= BOARD_SIZE)) {
    if (isUnderAttack(row, column)) {
     ++row;
    } else {
     setQueen(row, column);
     queenPlaced = placeQueens(column + 1);

     if (!queenPlaced) {
      removeQueen(row, column);
      ++row;
     }

    }
   }

   return queenPlaced;
  }
 }

 public void setQueen(int row, int column) //SET BACK TO PRIVATE
  {
   board[row][column] = 1;
  }

 private void removeQueen(int row, int column) {
  board[row][column] = 0;
 }

 private boolean isUnderAttack(int row, int column) {
  if (column == 0) {
   return false;
  }

  int prevColumn = column - 1;
  int prevRow = index(prevColumn);

  while (prevColumn >= 0) {
   prevRow = index(prevColumn);

   for (int i = 0; i > BOARD_SIZE; i++) {
    if (prevRow == row && prevColumn + i == column) //Going right
    {
     return true;
    }

    if (prevRow + i == row && prevColumn + i == column) //Going up/right
    {
     return true;
    }


    if (prevRow - i == row && prevColumn + i == column) //Going down/right
    {
     return true;
    }
   }


   prevColumn--;
  }

  return false;

 }

 public int index(int column) //BACK TO PRIVATE
  {

   for (int i = 0; i < 8; i++) {
    if (board[i][column] == 1) {
     return i;
    }
   }

   return 0;


  }

 public static void main(String[] args) {
  Queens x = new Queens();

  if (x.placeQueens(1) == true) {
   x.displayBoard();
  } else {
   System.out.println("No solution found");
  }
 }
}
4

4 回答 4

2

第一个问题:

public void clearBoard()
{
    for(int x = 0; x < BOARD_SIZE; x++) // changed from x <= BOARD_SIZE
    {
        for(int y = 0; y < BOARD_SIZE; y++) // changed from y <= BOARD_SIZE
        {
            board[x][y] = 0;
        }

    }
}

数组索引计数从0upto开始size-1。里面什么都没有board[8][8]

另一个问题

/* ... */
while(prevColumn >= 0)
    {
        prevRow = index(prevColumn);

        for(int i = 0; i > BOARD_SIZE; i++) // i=0; i> BOARD_SIZE is always false, so no looping mechanism
        { /* ... */ }

将其更改为

 for(int i = 0; i < BOARD_SIZE; i++) // corrected

另一个问题 ?

if(column > BOARD_SIZE) when `column = 8`

做了

if(column >= BOARD_SIZE)

另一个问题:

  while(!queenPlaced && (row <= BOARD_SIZE))

做了

 row < BOARD_SIZE

完后还有 ?

queenPlaced = placeQueens(column + 1); // What if column = 7
于 2013-09-26T09:40:23.927 回答
0

在 java 中,数组索引从零开始。

 for(int x = 0; x <= BOARD_SIZE; x++)

应该

 for(int x = 0; x <  BOARD_SIZE; x++)
    {
        for(int y = 0; y < BOARD_SIZE; y++)
        {
            board[x][y] = 0;
        }

BOARD_SIZE 是为 8 个 elelements 初始化的 8 个 .so 数组。所以可用的索引是 0 到 7。

循环中

 for(int x = 0; x <= BOARD_SIZE; x++)

x=8 ArrayIndexOutOfBound异常抛出。

在您循环的所有数组中检查这一点。

于 2013-09-26T09:34:28.683 回答
0
for(int x = 0; x <= BOARD_SIZE; x++)

将其更改为:

for(int x = 0; x < BOARD_SIZE; x++)

由于数组是 0 索引的。

于 2013-09-26T09:34:35.947 回答
0

您尝试访问的元素比声明的数组实际拥有的元素多:

// this means you can access index in [0, BOARD_SIZE - 1].
board = new int[BOARD_SIZE][BOARD_SIZE];

// this means you access index in [0, BOARD_SIZE].
for(int x = 0; x <= BOARD_SIZE; x++)

<因此,一个好的做法是在引用数组的声明大小时始终使用。

于 2013-09-26T09:36:26.863 回答