我遇到了创建随机数独网格的问题。我试着修改一个我用来解决这个难题的递归模式。谜题本身是一个二维整数数组。这是我(顺便说一句,该方法不只是随机的第一行我有一个想法,以随机的第一行,然后就决定做全网格):Sudoku递归问题(Java)
public boolean randomizeFirstRow(int row, int col){
Random rGen = new Random();
if(row == 9){
return true;
}
else{
boolean res;
for(int ndx = rGen.nextInt() + 1; ndx <= 9;){
//Input values into the boxes
sGrid[row][col] = ndx;
//Then test to see if the value is valid
if(this.isRowValid(row, sGrid) && this.isColumnValid(col, sGrid) && this.isQuadrantValid(row, col, sGrid)){
// grid valid, move to the next cell
if(col + 1 < 9){
res = randomizeFirstRow(row, col+1);
}
else{
res = randomizeFirstRow(row+1, 0);
}
//If the value inputed is valid, restart loop
if(res == true){
return true;
}
}
}
}
//If no value can be put in, set value to 0 to prevent program counting to 9
setGridValue(row, col, 0);
//Return to previous method in stack
return false;
}
此结果ArrayIndexOutOfBoundsException中带有可笑的高或低数字(+ - 100,000)。我试着看看它能走多远进入方法,而且也从未超出这条线:
if(this.isRowValid(row, sGrid) && this.isColumnValid(col, sGrid) && this.isQuadrantValid(row, col, sGrid))
我不明白数组索引如何去那么高。谁能帮我吗?
哇..我忘了添加参数。我非常愚蠢。 – SkylineAddict 2011-01-13 02:51:49