-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0051.N-Queens.swift
48 lines (40 loc) · 1.41 KB
/
0051.N-Queens.swift
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
class Solution {
private var result: [[String]] = [[String]]()
func solveNQueens(_ n: Int) -> [[String]] {
var board: [[Character]] = Array(repeating: Array(repeating: ".", count: n), count: n)
var queens = [(col: Int, row: Int)]()
queens.reserveCapacity(n)
tryPut(&board, 0, &queens)
return result
}
private func tryPut(_ board: inout [[Character]], _ row: Int, _ queens: inout [(col: Int, row: Int)]) {
if queens.count == board.count {
result.append(convertBoard(board))
return
}
for col in 0..<board[0].count {
if canPut(col, row, queens) {
queens.append((col, row))
board[col][row] = "Q"
tryPut(&board, row+1, &queens)
queens.removeLast()
board[col][row] = "."
}
}
}
private func canPut(_ col: Int, _ row: Int, _ queens: [(col: Int, row: Int)]) -> Bool {
for queen in queens {
if col == queen.col || row == queen.row || abs(col-queen.col) == abs(row-queen.row) {
return false
}
}
return true
}
private func convertBoard(_ board: [[Character]]) -> [String] {
var res = [String]()
for row in board {
res.append(String(row))
}
return res
}
}