Statistics

Problem Statement for "ChessKnight"

Problem Statement

The problem statement contains an image.

Consider an empty chess board (8x8 squares), with a chess knight placed on one of the squares. The possible movements of a chess knight are marked on the picture below.


If the knight moves n times, each time picking one of the eight directions uniformly at random (possibly a direction which makes the knight leave the chess board), determine the probability that it's still on the board after n jumps. Once the knight leaves the board, it can't enter it again.

Create a class ChessKnight containing the method probability which takes an int x, an int y (the start square of the chess knight, where 1,1 is one of the corners) and an int n, the number of jumps the knight will make. The method should return a double, the probability that the knight is still on the chess board after making n random jumps.

Definition

Class:
ChessKnight
Method:
probability
Parameters:
int, int, int
Returns:
double
Method signature:
double probability(int x, int y, int n)
(be sure your method is public)

Notes

  • Once the knight leaves the board, it can't enter it again.
  • Your return must have an absolute or relative error less than 1e-9.

Constraints

  • x will be between 1 and 8, inclusive.
  • y will be between 1 and 8, inclusive.
  • n will be between 1 and 100, inclusive.

Examples

  1. 1

    1

    2

    Returns: 0.1875

    When starting at a corner, only two of the initial jumps will cause the knight to stay on the board. From these new squares, six of the eight jumps will cause the knight to stay on the board. Hence the probability that the knight will stay on the board after two jumps is 1/4 * 6/8 = 0.1875.

  2. 4

    4

    1

    Returns: 1.0

    The knight can't fall off the board if only making a single jump from a square so close to the center.

  3. 2

    3

    10

    Returns: 0.0522148497402668

  4. 4

    3

    50

    Returns: 8.356427906809618E-7

  5. 3

    7

    1

    Returns: 0.75

  6. 7

    1

    1

    Returns: 0.375

  7. 8

    6

    2

    Returns: 0.359375

  8. 5

    8

    3

    Returns: 0.28515625

  9. 7

    1

    4

    Returns: 0.1533203125

  10. 1

    3

    5

    Returns: 0.149078369140625

  11. 2

    1

    6

    Returns: 0.0858612060546875

  12. 7

    8

    7

    Returns: 0.06415367126464844

  13. 6

    1

    8

    Returns: 0.06335264444351196

  14. 3

    6

    9

    Returns: 0.09343411028385162

  15. 7

    4

    52

    Returns: 3.5339651608739314E-7

  16. 2

    5

    3

    Returns: 0.427734375

  17. 5

    5

    66

    Returns: 9.547484435328105E-9

  18. 1

    7

    54

    Returns: 9.378667247847374E-8

  19. 5

    7

    14

    Returns: 0.018477533382110778

  20. 7

    5

    33

    Returns: 8.080721375391003E-5

  21. 2

    6

    70

    Returns: 1.8532254252166035E-9

  22. 5

    5

    73

    Returns: 1.2903706640250646E-9

  23. 2

    2

    98

    Returns: 4.612696185229347E-13

  24. 4

    1

    96

    Returns: 8.328005824672252E-13

  25. 6

    8

    67

    Returns: 2.990288315644292E-9

  26. 7

    7

    60

    Returns: 2.41174056768814E-8

  27. 3

    1

    96

    Returns: 7.496307086926672E-13

  28. 5

    8

    85

    Returns: 1.9336890083843252E-11

  29. 7

    7

    71

    Returns: 1.0386876797777846E-9

  30. 5

    6

    95

    Returns: 2.160083078770607E-12

  31. 5

    8

    6

    Returns: 0.12531280517578125

  32. 1

    1

    49

    Returns: 2.497810432608752E-7

  33. 3

    7

    69

    Returns: 2.4665846951278043E-9

  34. 4

    7

    68

    Returns: 3.6439947494962803E-9

  35. 6

    3

    55

    Returns: 1.8124424012141143E-7

  36. 6

    6

    98

    Returns: 8.299454779384809E-13

  37. 8

    8

    79

    Returns: 4.704635679653266E-11

  38. 6

    7

    36

    Returns: 3.0876840705236086E-5

  39. 7

    4

    60

    Returns: 3.588558274729162E-8

  40. 7

    5

    31

    Returns: 1.4314813598781563E-4

  41. 8

    5

    21

    Returns: 0.001710503378879963

  42. 3

    8

    94

    Returns: 1.3279537030090928E-12

  43. 6

    3

    5

    Returns: 0.29595947265625

  44. 5

    2

    34

    Returns: 6.071309186626193E-5

  45. 1

    7

    93

    Returns: 1.347713587933077E-12

  46. 8

    7

    75

    Returns: 2.3153555602356625E-10

  47. 2

    6

    22

    Returns: 0.0016903688946730915

  48. 1

    2

    15

    Returns: 0.006526388888801193

  49. 7

    6

    25

    Returns: 7.169329319809209E-4

  50. 7

    3

    71

    Returns: 1.392388626854472E-9

  51. 7

    4

    9

    Returns: 0.07716024667024612

  52. 7

    5

    65

    Returns: 8.591739291195658E-9

  53. 2

    5

    48

    Returns: 1.1090047187773974E-6

  54. 1

    8

    70

    Returns: 6.166461737819511E-10

  55. 2

    2

    50

    Returns: 4.20734780021499E-7

  56. 2

    3

    100

    Returns: 3.4905573073465814E-13

  57. 4

    2

    1

    Returns: 0.75

  58. 4

    3

    50

    Returns: 8.356427906809618E-7

  59. 4

    4

    100

    Returns: 5.730392258771815E-13

  60. 1

    1

    100

    Returns: 1.161455470367467E-13

  61. 3

    4

    100

    Returns: 5.171678774401681E-13

  62. 4

    3

    100

    Returns: 5.171678774401681E-13


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: