这就是我所拥有的。该方法应输出该空间中所有可用数字的数组。由于某些原因,这不会针对相同的框/行/列过滤掉。应该如何正确编码?
public int[] getAllowedValues(int row, int col){//both
int[] allowedValues = new int[9];
for(int i = 0; i < 9; i++){
allowedValues[i] = i;
}
for(int i = 0; i < 9; i++){
if(!(board[i][col] == 0)){
allowedValues[board[i][col]] = 0; //anything with a 0 is illegal, with number means that number is legal
}
if(!(board[row][i] == 0)){
allowedValues[board[row][i]] = 0; //anything with a 0 is illegal, with number means that number is legal
}
}
int rowStart = 0; //rowStart is the top left coord of box the number is in
if(row <= 3){
rowStart = 1;
} else if (row <= 6){
rowStart = 4;
} else if (row <= 9){
rowStart = 7;
}
int colStart = 0; //colStart is the top left coord of the box the number is in
if(col <= 3){
colStart = 1;
} else if (col <= 6){
colStart = 4;
} else if (col <= 9){
colStart = 7;
}
for(int i = rowStart; i < rowStart + 3; i++){
for(int j = colStart; j < colStart + 3; j++){
if(!(board[i-1][j-1] == 0)){
allowedValues[board[i-1][j-1]] = 0;
}
}
}
for(int i = row; i < 9; i++){
for(int j = col; j < 9; j++){
if((board[i][j] == 0)){
allowedValues[board[i][j]] = 0;
}
if((board[j][i] == 0)){
allowedValues[board[j][i]] = 0;
}
}
}
return allowedValues;
}
我将为您提供编码方法的步骤。
1)导入ArrayList
2)创建一个值为1到9的ArrayList
3)对于行中的每个数字,列数或3 x 3平方(因此不是允许的数量),请使用以下代码:
//x is a number in the row/collumn/square and al is the ArrayList
if(al.contains(x))al.remove(al.indexOf(x));
4)将方法类型从int []更改为ArrayList
5)返回ArrayList
如果您有任何困难或需要帮助,请告诉我。