JavaScript数独解算器。以前的号码是从哪里来的?
我正在分析bruteforce算法,有一个问题JavaScript数独解算器。以前的号码是从哪里来的?,javascript,solver,sudoku,Javascript,Solver,Sudoku,我正在分析bruteforce算法,有一个问题 var solveSudoku = function (grid, row, col) { var field = findUnassignedLocation(grid, row, col); row = field[0]; col = field[1]; if (row === -1) { if (!newGameStatus) fillTheDom(grid); return
var solveSudoku = function (grid, row, col) {
var field = findUnassignedLocation(grid, row, col);
row = field[0];
col = field[1];
if (row === -1) {
if (!newGameStatus) fillTheDom(grid);
return true;
}
for (var num = 1; num <= 9; num++) {
if (newGameStatus) {
num = Math.floor(Math.random() * 9) + 1;
}
if (isValid(grid, row, col, num)) {
console.log(row + ' ' + col)
grid[row][col] = num;
if (solveSudoku(grid, row, col)) {
return true;
}
console.log(row + ' ' + col)
grid[row][col] = 0;
}
}
return false;
}
var findUnassignedLocation = function (grid, row, col) {
var foundZero = false;
var location = [-1, -1];
while (!foundZero) {
if (row === 9) {
foundZero = true;
} else {
if (grid[row][col] === 0) {
location[0] = row;
location[1] = col;
foundZero = true;
} else {
if (col < 8) {
col++;
} else {
row++;
col = 0;
}
}
}
}
return location;
}
var solveSudoku=函数(网格、行、列){
var字段=findUnassignedLocation(网格、行、列);
行=字段[0];
col=字段[1];
如果(行==-1){
如果(!newGameStatus)填充了dom(网格);
返回true;
}
对于(var num=1;num,每次调用函数时,它的状态都会被保存(直到以下所有状态都失败),如果它们失败,处理器将跳回到最后一个分支,该分支至少有一个尚未失败的状态要转到,并继续,直到找到解决方案或所有状态都失败为止
我可以制作一个gif来尽可能简单地解释它,但我只能在每次调用函数时都在工作后这样做(直到以下所有状态都失败)若出现故障,处理器将跳回最后一个分支,该分支至少有一个尚未出现故障的状态,并继续执行,直到找到解决方案或所有操作都失败
我可以制作一个gif来尽可能简单地解释它,但我只能在工作后这样做想象递归就像格式化一样简单
solveSudoku(firstCell)
# Context : Global
On the first cell :
From 1 to 9 :
Is 1 valid ?
Yes.
If solveSudoku(nextCell):
# Context : We're in the loop of the first cell at iteration 1.
On the second cell :
From 1 to 9 :
Is 1 valid ?
No.
Is 2 valid ?
Yes.
If solveSudoku(nextCell):
# Context : We're in the loop of the second cell at iteration 2 which is in the loop of the first cell at iteration 1.
On the third cell :
From 1 to 9 :
Is 1 valid ?
No.
Is 2 valid ?
No.
Is 3 valid ?
No.
...
Is 9 valid ?
No.
Return false.
solveSudoku(nextCell = thirdCell) returned false, going on the loop. <<<<<<<<< This is "How does it goes back to the last cell?"
# Context : We're in the loop of the first cell at iteration 1.
Is 3 valid ?
Yes.
If solveSudoku(nextCell):
# Context : We're in the loop of the second cell at iteration 3 which is in the loop of the first cell at iteration 1.
On the third cell :
From 1 to 9 :
Is 1 valid ?
No.
Is 2 valid ?
Yes. <<<<<<< 2 is now valid and we can go on !
If solveSudoku(nextCell):
# Context : We're in the loop of the third cell at iteration 2 which is in the loop of the second cell at iteration 3 which is in the loop of the first cell at iteration 1.
On the fourth cell...
sudoku(firstCell)
#背景:全球
在第一个单元格上:
从1日至9日:
1有效吗?
对
如果是数独(nextCell):
#上下文:我们在迭代1的第一个单元的循环中。
在第二个单元格上:
从1日至9日:
1有效吗?
不
2有效吗?
对
如果是数独(nextCell):
#上下文:我们在迭代2的第二个单元的循环中,而在迭代1的第一个单元的循环中。
在第三个单元格上:
从1日至9日:
1有效吗?
不
2有效吗?
不
3号有效吗?
不
...
9有效吗?
不
返回false。
solveSudoku(nextCell=thirdCell)返回false,继续循环。想象递归就像格式化一样
solveSudoku(firstCell)
# Context : Global
On the first cell :
From 1 to 9 :
Is 1 valid ?
Yes.
If solveSudoku(nextCell):
# Context : We're in the loop of the first cell at iteration 1.
On the second cell :
From 1 to 9 :
Is 1 valid ?
No.
Is 2 valid ?
Yes.
If solveSudoku(nextCell):
# Context : We're in the loop of the second cell at iteration 2 which is in the loop of the first cell at iteration 1.
On the third cell :
From 1 to 9 :
Is 1 valid ?
No.
Is 2 valid ?
No.
Is 3 valid ?
No.
...
Is 9 valid ?
No.
Return false.
solveSudoku(nextCell = thirdCell) returned false, going on the loop. <<<<<<<<< This is "How does it goes back to the last cell?"
# Context : We're in the loop of the first cell at iteration 1.
Is 3 valid ?
Yes.
If solveSudoku(nextCell):
# Context : We're in the loop of the second cell at iteration 3 which is in the loop of the first cell at iteration 1.
On the third cell :
From 1 to 9 :
Is 1 valid ?
No.
Is 2 valid ?
Yes. <<<<<<< 2 is now valid and we can go on !
If solveSudoku(nextCell):
# Context : We're in the loop of the third cell at iteration 2 which is in the loop of the second cell at iteration 3 which is in the loop of the first cell at iteration 1.
On the fourth cell...
sudoku(firstCell)
#背景:全球
在第一个单元格上:
从1日至9日:
1有效吗?
对
如果是数独(nextCell):
#上下文:我们在迭代1的第一个单元的循环中。
在第二个单元格上:
从1日至9日:
1有效吗?
不
2有效吗?
对
如果是数独(nextCell):
#上下文:我们在迭代2的第二个单元的循环中,而在迭代1的第一个单元的循环中。
在第三个单元格上:
从1日至9日:
1有效吗?
不
2有效吗?
不
3号有效吗?
不
...
9有效吗?
不
返回false。
解算数独(nextCell=thirdCell)返回false,继续循环。这与javascript中的情况类似?每次调用函数时都会创建执行内容?每个执行内容都有自己的变量和状态?绝对是。每个函数调用都是独立的,都有自己的变量,它会传递参数,就这样!这与javascript中的情况类似?每次调用func时都会是否调用了执行内容?是否创建了执行内容?每个执行内容都有自己的变量和状态?是的,绝对是。每个函数调用都是独立的,都有自己的变量,它会传递参数,就是这样!