Probability that the knight stays on the board
InterviewTime limit2sMemory limit512 MB
A knight on an N by N board makes K random moves, each of the eight directions equally likely; find the probability it is still on the board after K moves.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Probability, Simulation, Implementation
- Solved
- No attempts yet
Problem
One knight stands on an chessboard. On every turn the knight picks one of eight directions, each with probability , and moves there.
Row 1 is the top row and row is the bottom row. Column 1 is the leftmost column and column is the rightmost column. The coordinate means row , column .
From the knight can move to , , , , , , , . A direction that leads off the board is picked with the same probability as any other.
Once the knight steps off the board it stops there and never gets back in. Given the starting square and the number of moves , write a program that computes the probability that the knight is still on the board after moves.
Input
The first line contains , the starting coordinates and of the knight, and the number of moves , separated by spaces. (, , )
Output
Print on the first line the probability that the knight is on the board after moves, with exactly 10 digits after the decimal point. The answer is a rational number whose denominator is , so round that exact value at the 11th digit after the decimal point, rounding a half up, and print all 10 digits. A probability of 1 prints as 1.0000000000 and a probability of 0 prints as 0.0000000000.