下面是我的solve方法。当我在我的main方法中调用它时,什么也没有发生,所有成功的东西都不会被执行,但是eclipse也不会报告错误。
public boolean solve(int r, int c){
if(c>8){
c=0;
r++;
}
if(r>8){
return true;
}
while(table[r][c].value!=0){
c++;
if(c>8){
c=-0;
r++;
}
if(r>8){
return true;
}
}
for(int k=1;k<10;k++){
if(table[r][c].checkRow(k)&&table[r][c].checkCol(k)&&table[r][c].checkCube(k)){
table[r][c].value=k;
solve(r,c);
}
}
table[r][c].value=0;
return false;
}这个算法会走回头路吗?若否,原因为何?
发布于 2015-05-08 00:46:37
它看起来像是一个逻辑错误,因此eclipse不会报告任何内容。
在代码的for循环部分中,应该有如下内容
for(int k=1;k<10;k++){
if(table[r][c].checkRow(k)&&table[r][c].checkCol(k)&&table[r][c].checkCube(k)){
table[r][c].value=k;
if(solve(r,c)){
return true;
}
table[r][c].value=0;
}
}在本例中,您将在for循环之外取消赋值该表,这将防止代码回溯。
Here是我解数独的代码。希望能有所帮助。
https://stackoverflow.com/questions/29424535
复制相似问题