Statistics

Problem Statement for "QueenInterference"

Problem Statement

This problem contains images.

In chess the queen reigns supreme over all other pieces. She has mastered the techniques of the rook and the bishop, thus allowing her to move any number of steps diagonally, vertically, or horizontally. Since a single queen controls such a large area, it is hard to put multiple queens on the board without them getting in each others' way (two queens are "in each others' way" if one can reach the other in a single move). For this reason there is a famous challenge requiring n queens to be placed on an n-by-n chess board such that no two queens interfere with each other.

In this problem you will implement a particular randomized solution. The board is setup as follows starting with column 1 on the left, and ending with column n on the right:
  • 1) Choose a random number R between 1 and n inclusive.
  • 2) Place a queen in row R of the current column (row 1 is the top; row n is the bottom).
An alteration step works as follows:
  • 1) Compute T, the number of columns containing reachable queens.
  • 2) Choose a random number K between 1 and T inclusive. Let C denote the Kth column, counting from the left, that contains a reachable queen. In effect, we have randomly chosen one of the reachable queens.
  • 3) For each of the n positions in column C, compute how many queens from other columns can currently reach that position.
  • 4) Move the queen in column C to the position in column C reachable by the fewest number of queens. If multiple positions are tied, continue to step 5, otherwise the alteration step is complete.
  • 5) Compute P, the number of positions that tied in step 4.
  • 6) Choose a random number Q between 1 and P inclusive. Counting from the top down, move the queen to the Qth of the tied positions in 4.
Note that an alteration step may not cause any movement at all. You will return how many alteration steps are required before no two queens interfere with one another. The ith random value used in the algorithms above will be (a(i) % Up)+1 where Up is the inclusive upper bound on the random number required and a(i) is given by the following formula:
  • a( 1 ) = 1 ,
  • a( k+1 ) = ( 16807 * a( k ) ) % 2147483647 for k > 0.
Here % denotes modulus or remainder. Use a 64-bit integral type to correctly compute the a(j) values. Random values 1 through n will be used to setup the board, while the remaining values will be used to perform the alteration steps. Make sure to follow the algorithm carefully thus ensuring each random value is used at the correct time. Specifically, make sure you do not continue on to step 5 unless directed to do so. If no alteration steps are required, return 0.

Definition

Class:
QueenInterference
Method:
numSteps
Parameters:
int
Returns:
int
Method signature:
int numSteps(int n)
(be sure your method is public)

Constraints

  • n must be between 4 and 100 inclusive.
  • n will not be 17.

Examples

  1. 4

    Returns: 34

  2. 5

    Returns: 4

    The first 5 random numbers are 2,3,5,4,4 thus producing the following initial configuration. Alteration Step 1: All 5 queens are reachable, so we choose a random number between 1 and 5 inclusive. The number we get is 1, so we shall work on the leftmost column. Computing reachabilities we arrive at the following scores. Since there are 3 positions reachable by 1 queen, we retrieve another random number between 1 and 3 inclusive. The number turns out to be 3 so the queen is placed on the lowest of the 3 potential spots. Alteration Step 2: Since only 4 queens are still reachable we choose a random number between 1 and 4 inclusive. The next two random values are 1 and 3 so the previous steps are repeated and the board remains unchanged. Alteration Step 3: Again we choose a random number between 1 and 4 inclusive, but this time we get 4. Column 5 is the fourth column containing a reachable queen. We thus compute how many queens can reach each position in that column. Row 2 is the least reachable, so the queen is moved there. Alteration Step 4: Now there are 3 reachable queens so we choose a random number between 1 and 3 inclusive. We get the number 2 thus causing us to alter column 3 (the second column from the left containing a reachable queen). Now we compute how many queens can reach each position. Only the topmost position is reachable by 0 queens so we move the queen there. Now the board is complete, so we return 4.

  3. 6

    Returns: 340

  4. 7

    Returns: 6

  5. 66

    Returns: 89

  6. 19

    Returns: 475

    The most possible steps.

  7. 20

    Returns: 90

  8. 30

    Returns: 53

  9. 40

    Returns: 60

  10. 50

    Returns: 79

  11. 15

    Returns: 38

  12. 14

    Returns: 32

  13. 55

    Returns: 41

  14. 9

    Returns: 8

  15. 10

    Returns: 17

  16. 100

    Returns: 148

  17. 6

    Returns: 340

  18. 8

    Returns: 101

  19. 11

    Returns: 254

  20. 16

    Returns: 146

  21. 19

    Returns: 475

  22. 25

    Returns: 101

  23. 27

    Returns: 304

  24. 29

    Returns: 120

  25. 31

    Returns: 127

  26. 32

    Returns: 108

  27. 33

    Returns: 156

  28. 35

    Returns: 105

  29. 45

    Returns: 132

  30. 47

    Returns: 122

  31. 49

    Returns: 147

  32. 51

    Returns: 136

  33. 53

    Returns: 122

  34. 56

    Returns: 121

  35. 62

    Returns: 149

  36. 65

    Returns: 177

  37. 69

    Returns: 129

  38. 70

    Returns: 163

  39. 72

    Returns: 163

  40. 73

    Returns: 106

  41. 74

    Returns: 127

  42. 75

    Returns: 161

  43. 76

    Returns: 101

  44. 77

    Returns: 112

  45. 79

    Returns: 126

  46. 80

    Returns: 155

  47. 81

    Returns: 190

  48. 83

    Returns: 123

  49. 84

    Returns: 170

  50. 85

    Returns: 137

  51. 86

    Returns: 115

  52. 87

    Returns: 138

  53. 88

    Returns: 173

  54. 89

    Returns: 127

  55. 90

    Returns: 102

  56. 91

    Returns: 103

  57. 92

    Returns: 116

  58. 93

    Returns: 152

  59. 94

    Returns: 169

  60. 95

    Returns: 146

  61. 96

    Returns: 102

  62. 97

    Returns: 150

  63. 98

    Returns: 141

  64. 99

    Returns: 136

  65. 100

    Returns: 148

  66. 7

    Returns: 6

  67. 19

    Returns: 475

  68. 100

    Returns: 148


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: