Problem Statement
Fix a coordinate system in the plane. We will now make a random walk according to the following rules: Start at the point (0,0). In each step randomly choose one of the four basic directions, and move by 1 in this direction. You are forbidden to walk along the same line segment twice (regardless of the direction). Should a random choice require you to do this, ignore it and make a new choice. The walk ends as soon as you reach the point (0,0) again.
To make our random choices, we will use a generator of pseudorandom numbers.
Our generator will work as follows:
Given is an
- Compute a new value xi+1 = (xi * 25173 + 13849) modulo 65536.
- Let d be the integer part of (xi+1/16384). In other words, d is given by the two most significant bits of xi+1.
-
The value d=0 corresponds to a move by (0,+1), denoted 'U'.
The value d=1 corresponds to a move by (0,-1), denoted 'D'.
The value d=2 corresponds to a move by (-1,0), denoted 'L'.
The value d=3 corresponds to a move by (+1,0), denoted 'R'.
A random walk can be uniquely described by a string of letters 'U', 'D', 'L', and 'R'.
The length of the walk is the number of steps made, or equivalently the number of letters
in its representation.
Write a method that will simulate the random walk and return its representation as a
If the length of the walk exceeds 40, return only the first 20 and the last 20 characters,
separated by three dots. (See Example #2.)
If the length of the walk exceeds 200,000, return an empty string.
Definition
- Class:
- RandomWalks
- Method:
- generateWalk
- Parameters:
- int
- Returns:
- String
- Method signature:
- String generateWalk(int seed)
- (be sure your method is public)
Notes
- You can not get stuck during the random walk.
- You are allowed to pass through a vertex twice, only using a previously used line segment is forbidden.
Constraints
- seed will be between 1 and 65,535, inclusive.
Examples
9
Returns: "LDRU"
This random seed leads to a simple random path with only four steps. For reference, the generated random values are x1=43798, x2=28775, x3=63052, and x4=5461.
21
Returns: "DLUR"
Again a path with four steps, but this time we have to ignore the third generated direction 'R'.
124
Returns: "RULUURDLLURULDLDLLLD...RURRUUURULDLDDDDRRRR"
The generated path for this seed has length 42. Note that we still use three dots in the returned String, even though only two characters are left out.
3
Returns: "DDRRURDDLURRDRRRRDDL...RDRULUURDLLLDDRDLLDD"
This path has length 6082.
2
Returns: ""
This path doesn't return to (0,0) soon enough.
107
Returns: "DRDLDDDRDDDRRURRRUUU...URDDDRDLLDDDLUUULLUL"
212
Returns: "LLDLDLLDLUURRUUUURDDDDLDDRURRDRDLULUURRU"
258
Returns: "DDRUUL"
14760
Returns: "LURULUURDLLLULLDDDRR...DLLURUUULLLDRRDDLULD"
14761
Returns: ""
25285
Returns: "DLULDLDLLUULURDRRDLD...LLULLDRDLDLLLLLLLLDD"
31649
Returns: "RDDLURRURRDRDRDRURUR...UURDRDDLUULLULLULLDD"
58053
Returns: "RULURURUULLULDRDDRUR...UULUURDRURUUUUUUUURR"
64417
Returns: "DRRULDDLDDRDRDRDLDLD...LLDRDRRULLUULUULUURR"
24
Returns: ""
47000
Returns: "DDDLULLUURRDLUURDR"
47013
Returns: "DLUR"
47026
Returns: "DRDRURULDLUUURRDDRRU...RRDRRUUULUULDRRDRDRR"
47039
Returns: "DDRRRURDLDLLDDRRRDDR...LULUUUULURURDRURDRUR"
47052
Returns: "DRDRUULDRRUURRRDRULUURDLLLURDDDLUULLLLDD"
47065
Returns: "DDDRRDLUUUURDLLLDLULDLLLDRUURURRDRUR"
47078
Returns: ""
47091
Returns: ""
47104
Returns: "DLDRDLULDLULLDLULLLL...URRRRULULULDRDLDDRDD"
47117
Returns: "DLUR"
47130
Returns: ""
47143
Returns: "DDRURDDRDRULURRDLUULLULL"
47156
Returns: "DLULLURRRD"
47169
Returns: "DRRURRULLDLL"
47182
Returns: "DDLLLDDDLLUURRRDRULU...RURULUUUULLDLULDRDDR"
47195
Returns: "DRURURURULLDRUURDRUL...DRRRURDDDDDRDLULLDRD"
47208
Returns: "DDDLLLULURUURDLLLULL...LURDDDRRDRUULULDDDRD"
47221
Returns: ""
47234
Returns: ""
47247
Returns: "ULURRDLUULURURUURRDR...ULLLLDDDRUURUULDDLLU"
47260
Returns: ""
47273
Returns: "ULDR"
47286
Returns: "URURRULLLULULDDRDDDR"
47299
Returns: ""
47312
Returns: ""
65532
Returns: ""
65533
Returns: ""
65534
Returns: ""
65535
Returns: "RRDLLLDDDDLLLULLLDDD...LURRUUULUURURDDRDDDD"
64123
Returns: "DLULULDDDRULLDRDLDRR...LLDDDDDLDDDDRRULDDDL"
61342
Returns: ""