-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51.n-皇后.js
66 lines (61 loc) · 1.45 KB
/
51.n-皇后.js
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
/*
* @lc app=leetcode.cn id=51 lang=javascript
*
* [51] N 皇后
*/
// @lc code=start
/**
* @param {number} n
* @return {string[][]}
*/
var solveNQueens = function(n) {
const res = []
const board = new Array(n).fill([]).map(item => new Array(n).fill('.'))
backtrack(board, 0)
return res
function backtrack(board, row) {
if (row === board.length) {
// remember to return the copy of the board
res.push(board.map(row => row.join('')))
return
}
const n = board[row].length
for (let col = 0; col < n; col++) {
// pick out invalid choice
if (!isValid(board, row, col)) {
continue
}
// make choice
board[row][col] = 'Q'
// backtrack
backtrack(board, row + 1)
// undo choice
board[row][col] = '.'
}
}
// check if can we place the queue at board[row][col]
// try to draw a 4 x 4 board on paper and you will see
function isValid(board, row, col) {
let n = board.length
// check current column
for (let i = row - 1; i >= 0; i--) {
if (board[i][col] === 'Q') {
return false
}
}
// check leftup
for (let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] === 'Q') {
return false
}
}
// check rightup
for (let i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
if (board[i][j] === 'Q') {
return false
}
}
return true
}
}
// @lc code=end