我写了一个javascript程序来解决数独。它似乎工作,但它突然停止。 Console shows that it is working but suddenly stops for no reason
这是什么原因? :(Timeline)我是javascript的新手,我该怎么办才能修复它?
我该如何解决?谢谢!
这是代码:
<html>
<head>
</head>
<body>
<script>
var game = [
[0, 0, 9, 0, 5, 7, 0, 6, 0],
[0, 7, 0, 6, 0, 0, 0, 0, 0],
[8, 0, 0, 0, 1, 0, 7, 5, 0],
[0, 0, 0, 3, 6, 0, 1, 7, 2],
[4, 1, 2, 0, 0, 0, 6, 9, 3],
[6, 3, 7, 0, 2, 9, 0, 0, 0],
[0, 4, 1, 0, 5, 0, 0, 0, 7],
[0, 0, 0, 0, 0, 3, 0, 8, 0],
[0, 5, 0, 2, 9, 0, 4, 0, 0]
];
function solve(x, y, g) {
var nextx;
var nexty = y;
if (x < 9 || y < 9) {
if (g[x][y] == 0) {
for (i = 1; i < 10; i++) {
g[x][y] = i;
if (check(x, y, i, g) == true) {
if (x < 8) {
nextx = x + 1;
} else {
nexty = y + 1;
nextx = 0;
}
console.log(x + ' ' + y + ' ' + i + ' ' + g[x][y]);
solve(nextx, nexty, g);
} else {
if (x != 0 && y != 0) {
return;
}
}
}
} else {
if (x < 8) {
nextx = x + 1;
} else {
nexty = y + 1;
nextx = 0;
}
console.log(x + ' ' + y + ' ' + 'b' + ' ' + g[x][y]);
solve(nextx, nexty, g);
return;
}
} else {
if (checkall(g) == true) {
for (var i = 0; i < 9; i++) {
document.write(g[i] + '</br>');
};
} else {
document.write("can't solve");
}
}
}
function check(x, y, i, g) {
var tempx = 0;
var tempy = 0;
var tempb = 0;
var a, b;
for (j = 0; j < 9; j++) {
if (g[x][j] == i) {
tempx = tempx + 1;
}
if (g[j][y] == i) {
tempy = tempy + 1;
}
}
if (x < 3) {
a = 0;
}
if (x < 6) {
a = 3;
}
if (x < 9) {
a = 6;
}
if (y < 3) {
b = 0;
}
if (y < 6) {
b = 3;
}
if (y < 9) {
b = 6;
}
for (c = a; c < a + 3; c++) {
for (d = b; d < b + 3; d++) {
if (g[a][b] == i) {
tempb = tempb + 1;
}
}
}
if (tempx > 1 || tempy > 1 || tempb > 1) {
return false;
} else {
return true;
}
}
function checkall(g) {
var temp1 = 0;
var temp2 = 0;
for (var i = 0; i < 10; i++) {
for (var x = 0; x < 9; x++) {
for (var y = 0; y < 9; y++) {
if (g[x][y] == i) {
temp1 = temp1 + 1;
}
if (g[y][x] == i) {
temp2 = temp2 + 1;
}
};
if (temp1 > 1 || temp2 > 1) {
return false;
} else {
temp1 = 0;
temp2 = 0;
}
}
}
return true
}
solve(0, 0, game);
</script>
</div>
</body>
</html>
确保检查函数返回的响应:
var div = document.getElementById('output');
将失败并且div将被“未定义”,因为您的示例中没有打开div标记,并带有'output'id。
如果将try / catch子句添加到函数中,则会看到报告的错误。
我可以看到你实际上并没有在代码中使用'div'。
我刚刚修改了你的代码,因为我在运行它时没有遇到任何错误:
<html>
<body>
<script>
var game = [
[0, 0, 9, 0, 5, 7, 0, 6, 0],
[0, 7, 0, 6, 0, 0, 0, 0, 0],
[8, 0, 0, 0, 1, 0, 7, 5, 0],
[0, 0, 0, 3, 6, 0, 1, 7, 2],
[4, 1, 2, 0, 0, 0, 6, 9, 3],
[6, 3, 7, 0, 2, 9, 0, 0, 0],
[0, 4, 1, 0, 5, 0, 0, 0, 7],
[0, 0, 0, 0, 0, 3, 0, 8, 0],
[0, 5, 0, 2, 9, 0, 4, 0, 0]
];
var intNestedCalls = 0;
function solve(x, y, g) {
var nextx;
var nexty = y;
intNestedCalls++; //Count numer of nested calls
if (x < 9 || y < 9) {
if (g[x][y] == 0) {
for (i = 1; i < 10; i++) {
g[x][y] = i;
if (check(x, y, i, g) == true) {
if (x < 8) {
nextx = x + 1;
} else {
nexty = y + 1;
nextx = 0;
}
console.log(x + ' ' + y + ' ' + i + ' ' + g[x][y]);
solve(nextx, nexty, g);
} else {
if (x != 0 && y != 0) {
//Break here so we drop out and exit the function
break;
}
}
}
} else {
if (x < 8) {
nextx = x + 1;
} else {
nexty = y + 1;
nextx = 0;
}
console.log(x + ' ' + y + ' ' + 'b' + ' ' + g[x][y]);
solve(nextx, nexty, g);
//No need to return here, fall through and exit normally
// return;
}
} else {
if (checkall(g) == true) {
for (var i = 0; i < 9; i++) {
document.write(g[i] + '</br>');
};
} else {
document.write("can't solve");
}
}
intNestedCalls--; //Update nested call counter
}
function check(x, y, i, g) {
var tempx = 0;
var tempy = 0;
var tempb = 0;
var a, b;
for (j = 0; j < 9; j++) {
if (g[x][j] == i) {
tempx = tempx + 1;
}
if (g[j][y] == i) {
tempy = tempy + 1;
}
}
if (x < 3) {
a = 0;
}
if (x < 6) {
a = 3;
}
if (x < 9) {
a = 6;
}
if (y < 3) {
b = 0;
}
if (y < 6) {
b = 3;
}
if (y < 9) {
b = 6;
}
for (c = a; c < a + 3; c++) {
for (d = b; d < b + 3; d++) {
if (g[a][b] == i) {
tempb = tempb + 1;
}
}
}
if (tempx > 1 || tempy > 1 || tempb > 1) {
return false;
} else {
return true;
}
}
function checkall(g) {
var temp1 = 0;
var temp2 = 0;
for (var i = 0; i < 10; i++) {
for (var x = 0; x < 9; x++) {
for (var y = 0; y < 9; y++) {
if (g[x][y] == i) {
temp1 = temp1 + 1;
}
if (g[y][x] == i) {
temp2 = temp2 + 1;
}
};
if (temp1 > 1 || temp2 > 1) {
return false;
} else {
temp1 = 0;
temp2 = 0;
}
}
}
return true;
}
solve(0, 0, game);
alert("Complete!");
</script>
</div>
</body>
</html>
除了完整之外,我什么都看不到输出!在短时间内提醒。
逻辑永远不会得到你的'document.write'指令,因为如果x和y> = 9,你的逻辑x和y只会到达write语句。
你的测试应该是:
if ( x < 9 && y < 9 ) {
这是我第一次评论这个网站但是有可能在js中运行一个强力的数独求解器,因为没有1e50个可能的排列,因为这包括每个数字可能是1的情况。实际上有一个数独的6670903751057913937920解决方案。这个数字只减少了1e27倍。但是一个蛮力的程序甚至不会接近这个数字。相反,由于给定的值,它在你有一个解决的数独之前通常最多运行10000次。这是我制作的代码以及它停止的原因是因为数据暗示你插入了没有解决方案。
<!DOCTYPE html>
<html>
<body>
<script>
var board = [
[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9],
];
function solve(a){
var zeros = [];
for(i=0;i<Math.pow(a.length,2);i++){
var x = i%a.length;
var y = (i-x)/a.length;
if(board[y][x]===0){
zeros.push([x,y,1]);
}
}
var count = 0;
var total = [];
for(i=0;i<zeros.length;i++){
console.log("s");
count+=1;
var x = zeros[i][0];
if(zeros[i][2]<board.length+1){
for(j=zeros[i][2];j<board.length+1;j++){
if(check(x,y,j)==true){
a[y][x] = j;
zeros[i][2] = j + 1;
j=board.length+1;
}else if(check(x,y,j)==false && j===board.length){
j=board.length+1;
zeros[i][2] = 1;
i-=2;
a[y][x] = 0;
}
}
}else{
zeros[i][2] = 1;
a[y][x] = 0;
i-=2;
}
for(j=0;j<board.length;j++){
console.log(board[j][0],board[j][1],board[j][2],board[j][3],board[j][4],board[j][5],board[j][6],board[j][7],board[j][8]);
}
console.log(count);
}
}
function check(a,b,c){
var right = true;
for(k=0;k<board.length;k++){
if(board[b][k]===c || board[k][a]===c){
right = false;
}
}
for(k=0;k<Math.sqrt(board.length);k++){
for(l=0;l<Math.sqrt(board.length);l++){
if(board[Math.sqrt(board.length)*Math.floor(b/Math.sqrt(board.length))+k][Math.sqrt(board.length)*Math.floor(a/Math.sqrt(board.length))+l]==c){
right = false;
}
}
}
if(right === true){
return true;
}else{
return false;
}
}
solve(board);
</script>
</body>
</html>