Under construction

Still being built. Things may change

Probability that the knight stays on the board

Interview

Time limit2sMemory limit512 MB

Summary
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 N×NN \times N chessboard. On every turn the knight picks one of eight directions, each with probability 1/81/8, and moves there.

Row 1 is the top row and row NN is the bottom row. Column 1 is the leftmost column and column NN is the rightmost column. The coordinate (x,y)(x, y) means row xx, column yy.

From (x,y)(x, y) the knight can move to (x+1,y+2)(x+1, y+2), (x+2,y+1)(x+2, y+1), (x+2,y−1)(x+2, y-1), (x+1,y−2)(x+1, y-2), (x−1,y−2)(x-1, y-2), (x−2,y−1)(x-2, y-1), (x−2,y+1)(x-2, y+1), (x−1,y+2)(x-1, y+2). 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 KK, write a program that computes the probability that the knight is still on the board after KK moves.

Input

The first line contains NN, the starting coordinates xx and yy of the knight, and the number of moves KK, separated by spaces. (1≤N≤501 \le N \le 50, 1≤x,y≤N1 \le x, y \le N, 0≤K≤500 \le K \le 50)

Output

Print on the first line the probability that the knight is on the board after KK moves, with exactly 10 digits after the decimal point. The answer is a rational number whose denominator is 8K8^K, 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.

Examples9

  1. Example 1

    Input
    1 1 1 0
    
    Expected output
    1.0000000000
    
  2. Example 2

    Input
    1 1 1 1
    
    Expected output
    0.0000000000
    
  3. Example 3

    Input
    1 1 1 2
    
    Expected output
    0.0000000000
    
  4. Example 4

    Input
    8 1 1 1
    
    Expected output
    0.2500000000
    
  5. Example 5

    Input
    8 1 1 2
    
    Expected output
    0.1875000000
    
  6. Example 6

    Input
    8 1 1 3
    
    Expected output
    0.1250000000
    
  7. Example 7

    Input
    8 1 1 4
    
    Expected output
    0.0986328125
    
  8. Example 8

    Input
    8 1 1 10
    
    Expected output
    0.0173958503
    
  9. Example 9

    Input
    50 1 1 50
    
    Expected output
    0.0092398880