-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.kt
42 lines (36 loc) · 1007 Bytes
/
solution.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
import kotlin.math.*
import java.util.*
class ChessBoard(n: Int) {
val queens = IntArray(n) { 0 }
var currentY = 0
fun canKill(thisY: Int, otherY: Int): Boolean {
val crossX = abs(queens[thisY] - queens[otherY])
val crossY = abs(thisY - otherY)
return queens[thisY] == queens[otherY] || thisY == otherY || crossX == crossY
}
inline fun canPlace(x: Int, f: (ChessBoard) -> Unit) {
queens[currentY] = x
if ((0 until currentY).all { !canKill(it, currentY) }) {
currentY += 1
f(this)
currentY -= 1
}
queens[currentY] = 0
}
}
fun solve(n: Int, y: Int, board: ChessBoard): Int {
if (y >= n) {
return 1
}
var result = 0
for (x in 0 until n) {
board.canPlace(x) {
result += solve(n, y + 1, board)
}
}
return result
}
fun main(args: Array<out String>) {
val n = readLine()!!.toInt()
println(solve(n, 0, ChessBoard(n)))
}