LeetCode Entry
688. Knight Probability in Chessboard
Probability of making k steps on a chessboard without stepping outside
688. Knight Probability in Chessboard medium
blog post
substack

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/283
Problem TLDR
Probability of making k steps on a chessboard without stepping outside
Intuition
The description example doesnβt give a clear picture of how the probability works.

- individual probability is
1/8each time we make a step. -
- One step is
1/8, two steps are1/8 * 1/8and so on.
- One step is
-
- So, the
k-stepspath will have probability of1/8^k
- So, the
- we need to sum all the probabilities of individual
k-stepspaths, that will remain on a board -
- the brute force algorithm for this will be BFS:
-
-
- for
krounds:
- for
-
-
-
-
- poll all the elements, and make possible steps on the board
-
-
-
-
-
- resulting probability will be
queue.size / 8^k, as queue will contain only the final possible ways after k steps
- resulting probability will be
-
-
However, there are too many possible ways, we will quickly run out of memory.
It is noticeable, some ways are repeating, and after s steps the same cell [x, y] produces the same amount of possible ways dp[x, y][s]. We can cache this result for each cell.
However, the number of ways are still very big and do not fit into Long 64 bits. To solve this, we can cache not only the ways, but the probability, dividing each step by 8.
Approach
- storing the directions in a sequence helps to reduce some LOC
Complexity
-
Time complexity: \(O(kn^2)\)
-
Space complexity: \(O(kn^2)\)
Code
val dxdy = sequenceOf(-2 to 1, -2 to -1, 2 to 1, 2 to -1, -1 to 2, -1 to -2, 1 to 2, 1 to -2)
fun knightProbability(n: Int, k: Int, row: Int, column: Int): Double {
val cache = mutableMapOf<Pair<Pair<Int, Int>, Int>, Double>()
fun count(x: Int, y: Int, k: Int): Double = if (k == 0) 1.0 else cache.getOrPut(x to y to k) {
dxdy.map { (dx, dy) -> x + dx to y + dy }
.map { (x, y) -> if (x in 0..n-1 && y in 0..n-1) count(x, y, k - 1) / 8.0 else 0.0 }
.sum()
}
return count(column, row, k)
}
The magical rundown
Step β - The High Noon Duel π€ π΅π΅:
πΆ The town clock strikes twelve, and the high noon chess duel commences. A
lone knight π trots onto the scorching, sun-bleached chessboard, casting a long
shadow on the sandy squares.
ββββπ΅βββπ΅ββββ
β π β β β
β βββπ΅βββπ΅ββββ£
β β β β
β βββπ΅βββπ΅ββββ£
β β β β
ββββπ΅βββπ΅ββββ
The Sheriff π€ , ever the statistician, watches keenly. "For now, the odds are
all in your favor, Knight," he says, unveiling the initial probability πΉβ = 1.
ββββββπ°βββββ¬ββββπ°βββββ¬ββββπ°βββββ
β 1 β 0 β 0 β
ββββββπ°βββββΌββββπ°βββββΌββββπ°βββββ€
β 0 β 0 β 0 β
ββββββπ°βββββΌββββπ°βββββΌββββπ°βββββ€
β 0 β 0 β 0 β
ββββββπ°βββββ΄ββββπ°βββββ΄ββββπ°βββββ
Step β - The Dusty Trail ππ΅π΄:
πΆ The knight π leaps into action, stirring up a cloud of dust. He lands in two
different squares, each with a calculated 1/8 chance. The Sheriff π€ nods
approvingly. "Bold moves, Knight. The probability after this is πΉβ = 1/8 + 1/8 = 1/4."
ββββπ΅βββπ΅ββββ
β β β β
β βββπ΅βββπ΅ββββ£
β β β π β
β βββπ΅βββπ΅ββββ£
β β π β β
ββββπ΅βββπ΅ββββ
He reveals the new odds:
ββββββπ°βββββ¬ββββπ°βββββ¬ββββπ°βββββ
β 0 β 0 β 0 β
ββββββπ°βββββΌββββπ°βββββΌββββπ°βββββ€
β 0 β 0 β ΒΉ/β β
ββββββπ°βββββΌββββπ°βββββΌββββπ°βββββ€
β 0 β ΒΉ/β β 0 β
ββββββπ°βββββ΄ββββπ°βββββ΄ββββπ°βββββ
Step β - The Sun-Baked Crossroads βοΈπ΅πͺοΈ:
πΆ The knight π continues his daring maneuvers, hopping onto a few critical
spots. He lands on three squares, with probabilities of 1/64, 1/64, and 2/64.
Adding these up, the Sheriff π€ declares, "The stakes have risen, Knight. The
total is πΉβ = 1/64 + 1/64 + 2/64 = 1/16."
ββββπ΅βββπ΅ββββ
βππβ β π β
β βββπ΅βββπ΅ββββ£
β β β β
β βββπ΅βββπ΅ββββ£
β π β β β
ββββπ΅βββπ΅ββββ
The updated odds take shape:
ββββββπ°βββββ¬ββββπ°βββββ¬ββββπ°βββββ
β Β²/ββ β 0 β ΒΉ/ββ β
ββββββπ°βββββΌββββπ°βββββΌββββπ°βββββ€
β 0 β 0 β 0 β
ββββββπ°βββββΌββββπ°βββββΌββββπ°βββββ€
β ΒΉ/ββ β 0 β 0 β
ββββββπ°βββββ΄ββββπ°βββββ΄ββββπ°βββββ
Step β - The Outlaw's Hideout ποΈπ΅π:
πΆ As the sun sets, the knight π lands in a few hidden spots with various
probabilities. Each calculated leap adds to his total: 1/512 + 1/512 + 3/512 + 3/512.
The Sheriff π€ raises an eyebrow. "Well played, Knight. Your total now is πΉβ =
1/512 + 1/512 + 3/512 + 3/512."
ββββπ΅βββπ΅ββββ
β β π β β
β βββπ΅βββπ΅ββββ£
β π β βπππβ
β βββπ΅βββπ΅ββββ£
β βπππβ β
ββββπ΅βββπ΅ββββ
Beneath the twinkling stars, the Sheriff π€ surveys the evolving game. "You're
not an easy one to beat, Knight," he admits, revealing the updated stakes:
ββββββπ°βββββ¬ββββπ°βββββ¬ββββπ°βββββ
β 0 β ΒΉ/β
ββ β 0 β
ββββββπ°βββββΌββββπ°βββββΌββββπ°βββββ€
β ΒΉ/β
ββ β 0 β Β³/β
ββ β
ββββββπ°βββββΌββββπ°βββββΌββββπ°βββββ€
β 0 β Β³/β
ββ β 0 β
ββββββπ°βββββ΄ββββπ°βββββ΄ββββπ°βββββ
πΆ So, under the twinkling stars and to the tune of the whistling wind, our
knight's adventure continues into the night. The stakes are high, the moves
unpredictable, but one thing's certain: this wild chess duel is far from over! π΅πππ΅