【发布时间】:2014-05-09 20:50:30
【问题描述】:
我目前正在编写一些代码来解决作业中的数独难题。我目前编写的代码应该是隔离我们需要插入值的行和列,然后测试值 1-9 以查看哪些已经出现在行或列中。问题是,二进制搜索没有检测到一些数字,我不知道为什么。这是我的代码。你需要做的只是 A.grid 是一个包含未解决难题的数组,而设置值方法是我编写的一种方法,用于使用 fromat setValue(xLocation, yLocation, value) 将值插入到数组中
int currentVal = 0;
int j;
int [] currentRow = new int [9];
int [] currentCol = new int [9];
for (i = 0; i<9 ; i++) {
for (j = 0; j<9 ; j++){
currentVal=A.grid[i][j];
boolean keepGoing = true;
int newVal=1;
if (currentVal==0) {
for( int k = 0; k < 9; k++)
{currentCol[k] = A.grid[k][j];
}
currentRow = A.grid[i];
log(Arrays.toString(currentRow));
log(Arrays.toString(currentCol));
log("");
while (keepGoing) {
int indexRow=Arrays.binarySearch(currentRow, newVal);
int indexCol=Arrays.binarySearch(currentCol, newVal);
log(indexRow);
log(indexCol);
log("");
if (indexRow<0 && indexCol<0) {
keepGoing = false;
}
newVal++;
}
A.setValue(j, i, newVal-1);
谢谢!
【问题讨论】: