Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find the percentage of places where knight can move and not left the board in Python
Suppose we have four values n, x, y, and k. Here n indicates an n x n chessboard and (x, y) coordinates represent where a knight is placed. The knight has to take exactly k steps, where at each step it can move in any of the 8 directions with equal probability. We have to find the percentage chance (nearest integer) that the knight remains on the chessboard after taking k moves.
The key condition is that once the knight leaves the board, it cannot enter again.
Example Scenario
If the input is n = 8, (x = 0, y = 0), k = 1, then the output will be 25. Here we have an 8x8 chessboard and the initial position of the knight is (0, 0). From corner position (0, 0), the knight can only make 2 valid moves out of 8 possible moves to stay on the board, giving us 25% probability.
Algorithm
We use dynamic programming with recursion to calculate the probability ?
- Define all 8 possible knight moves: [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)]
- Use DFS to explore all possible paths for k steps
- If the knight goes outside the board, return 0 (no chance of staying)
- If k steps are completed and knight is still on board, return 1
- For each position, calculate average probability across all 8 moves
Implementation
class Solution:
def solve(self, n, x, y, k):
# All 8 possible knight moves
moves = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)]
def dfs(x, y, k):
# If knight is outside the board
if x < 0 or y < 0 or x >= n or y >= n:
return 0
# If no more steps left and knight is still on board
if k == 0:
return 1
# Calculate probability for all possible moves
total_probability = 0
for dx, dy in moves:
total_probability += dfs(x + dx, y + dy, k - 1) / 8
return total_probability
# Return percentage as nearest integer
return round(dfs(x, y, k) * 100)
# Test the solution
ob = Solution()
n = 8
x = 0 # Starting at corner position
y = 0
k = 1
result = ob.solve(n, x, y, k)
print(f"Percentage chance knight stays on board: {result}%")
Percentage chance knight stays on board: 25%
How It Works
The algorithm uses recursive depth-first search with memoization concept ?
- Base Cases: If knight moves outside board, probability is 0. If k=0 and still on board, probability is 1
- Recursive Case: For each of 8 possible moves, calculate probability recursively
- Probability Calculation: Each move has 1/8 probability, so we divide by 8
- Final Result: Multiply by 100 and round to get percentage
Example with Different Starting Position
# Test from center position
ob = Solution()
n = 8
x = 4 # Center position
y = 4
k = 1
result = ob.solve(n, x, y, k)
print(f"From center (4,4): {result}% chance")
# Test from edge position
x = 0
y = 3
result = ob.solve(n, x, y, k)
print(f"From edge (0,3): {result}% chance")
From center (4,4): 100% chance From edge (0,3): 50% chance
Conclusion
This solution uses recursive probability calculation to determine the knight's chance of staying on the chessboard. The probability depends on the starting position and number of moves, with corner positions having lower success rates than center positions.
