-
Notifications
You must be signed in to change notification settings - Fork 0
/
NQueens.kt
58 lines (48 loc) · 1.75 KB
/
NQueens.kt
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
package leetcode
/**
* Problem description on [LeetCode](https://leetcode.com/problems/n-queens/)
*/
class NQueens {
private val answers = mutableListOf<List<String>>()
private lateinit var isEmptyVertical: BooleanArray
private lateinit var isEmptyDiagonal1: BooleanArray
private lateinit var isEmptyDiagonal2: BooleanArray
fun solveNQueens(n: Int): List<List<String>> {
val current = IntArray(n) { EMPTY_CELL }
isEmptyVertical = BooleanArray(n) { true }
isEmptyDiagonal1 = BooleanArray(2 * n) { true }
isEmptyDiagonal2 = BooleanArray(2 * n) { true }
backtrack(n, 0, current)
return answers
}
private fun backtrack(n: Int, row: Int, current: IntArray) {
if (row >= n) {
answers.add(current.toAnswer())
return
}
for (col in 0 until n) {
val diagonal1Index = n + col - row - 1
val diagonal2Index = 2 * n - col - row - 2
if (isEmptyVertical[col] &&
isEmptyDiagonal1[diagonal1Index] &&
isEmptyDiagonal2[diagonal2Index]
) {
isEmptyVertical[col] = false
isEmptyDiagonal1[diagonal1Index] = false
isEmptyDiagonal2[diagonal2Index] = false
current[row] = col
backtrack(n, row + 1, current)
current[row] = EMPTY_CELL
isEmptyVertical[col] = true
isEmptyDiagonal1[diagonal1Index] = true
isEmptyDiagonal2[diagonal2Index] = true
}
}
}
private fun IntArray.toAnswer(): List<String> = map {
".".repeat(size).replaceRange(it, it + 1, "Q")
}
companion object {
private const val EMPTY_CELL = -1
}
}