function queen(n) {
var count = 0;
var arr = [];
var row = 0;
visit(arr, row);
return count;
function visit(arr, row) {
for (var i = 0; i < n; i++) {
arr[row] = i;
if (check(arr, row)) {
if (row == n - 1) {
// console.log(arr);
count++;
} else {
visit(arr, row + 1);
}
}
}
function check(arr, row) {
for (var i = 0; i < row; i++) {
if (arr[row] == arr[i] || Math.abs((row - i) / (arr[row] - arr[i])) == 1) {
return false;
}
}
return true;
}
}
}
console.log(queen(8));
递归求解八皇后
最新推荐文章于 2021-03-18 12:54:15 发布