-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.html
109 lines (92 loc) · 3.06 KB
/
script.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<script code-main>
/********** 基础版 **********/
function eightQueens(row, length, matrix) {
if (row > length - 1) {
printResult(matrix);
return;
}
for (let column = 0; column < length; ++column) {
if (checkQueen(row, column, length, matrix)) {
matrix[row][column] = 1;
eightQueens(row + 1, length, matrix);
matrix[row][column] = 0;
}
}
}
let resultCount = 0;
function printResult(matrix) {
console.log(`result no: ${++resultCount}\r\n` + matrix.map(row => row.join(', ')).join('\r\n'));
}
function checkQueen(row, column, length, matrix) {
for (let i = 0; i < length; ++i) {
if (i !== column && matrix[row][i]) {
return false;
}
if (i !== row && matrix[i][column]) {
return false;
}
}
for (let i = 0; i < length; ++i) {
if (i !== row) {
const diff = i - row;
if (column - diff >= 0 && column - diff < length && matrix[i][column - diff]) {
return false;
}
if (column + diff >= 0 && column + diff < length && matrix[i][column + diff]) {
return false;
}
}
}
return true;
}
const matrix = [
[0, 0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, 0, ],
];
eightQueens(0, 8, matrix);
/********** 标记优化版 **********/
let resultCount2 = 0;
function eightQueens2(row, length, signCol, signLeft, signRight) {
if (row >= length) {
++resultCount2;
return;
}
for (let column = 0; column < length; ++column) {
if (checkQueen2(row, column, signCol, signLeft, signRight)) {
setQueen(row, column, signCol, signLeft, signRight);
eightQueens2(row + 1, length, signCol, signLeft, signRight);
deleteQueen(row, column, signCol, signLeft, signRight);
}
}
}
function checkQueen2(row, column, signCol, signLeft, signRight) {
if (signCol[column]) {
return false;
}
if (signLeft[row + column]) {
return false;
}
if (signRight[row - column]) {
return false;
}
return true;
}
function setQueen(row, column, signCol, signLeft, signRight) {
signCol[column] = 1;
signLeft[row + column] = 1;
signRight[row - column] = 1;
}
function deleteQueen(row, column, signCol, signLeft, signRight) {
signCol[column] = 0;
signLeft[row + column] = 0;
signRight[row - column] = 0;
}
eightQueens2(0, 8, {}, {}, {}, {});
console.log('count2:', resultCount2);
</script>