Problem Statement
Time limit: 4 seconds.
The infinite grid graph is a graph whose vertices are all pairs of integers, and two vertices (x1,y1) and (x2,y2) are connected by an edge if and only if abs(x1-x2) + abs(y1-y2) = 1.
We can visualize such a graph as follows: Vertices are points in a horizontal plane with integer coordinates. We can draw two lines through each point, each of them parallel to one of the coordinate axes. For each of these lines, each pair of consecutive vertices on the line is connected by an edge.
You start a random walk in the vertex (0, 0).
In each step of the walk you traverse one of the four edges that lead from your current vertex.
These four edges point north (y increases), south (y decreases), east (x increases), and west (x decreases).
You are given the
Your walk terminates as soon as you traverse the M-th distinct edge.
Calculate and return the expected number of steps in your random walk.
Definition
- Class:
- VisitEdges
- Method:
- solve
- Parameters:
- int, int, int, int, int
- Returns:
- double
- Method signature:
- double solve(int M, int N, int S, int E, int W)
- (be sure your method is public)
Notes
- The meaning of "relative probabilities" is as follows: the probability of moving north in any step is N / (N + S + E + V).
- Return values with an absolute or a relative error at most 1e-9 will be accepted.
Constraints
- M will be between 1 and 7, inclusive.
- N, S, E, and W will each be between 0 and 10, inclusive.
- At least one of N, S, E, and W will be positive.
Examples
3
1
0
0
0
Returns: 3.0
In each step we go north. In three steps we are guaranteed to traverse three distinct edges.
3
1
1
0
0
Returns: 6.0
In each step we flip a fair coin whether we go north or south. In this setting visiting three distinct edges might take longer, as, for example, we can start by going north, south, and north again. We just performed three steps but we are still not done: we have still only traversed one edge (three times).
3
1
1
1
1
Returns: 3.761904761904762
Perhaps paradoxically, adding even more possible directions now somewhat decreases the expected number of steps.
1
4
5
6
7
Returns: 1.0
Regardless of the probabilities, in the first step we are guaranteed to traverse a new edge.
4
4
5
6
7
Returns: 5.276493355280059
7
4
5
6
7
Returns: 9.949621352068304
7
4
0
0
7
Returns: 7.0
1
0
0
7
0
Returns: 1.0
4
0
1
2
1
Returns: 5.2436509664841555
6
1
1
2
1
Returns: 8.111115394357354
5
2
0
1
2
Returns: 5.868050513099962
2
0
0
1
1
Returns: 3.0
7
2
0
1
2
Returns: 8.293464333680454
5
0
0
2
2
Returns: 14.999999999999996
4
1
0
0
2
Returns: 4.0
4
2
2
1
2
Returns: 5.191805258407552
4
2
2
0
0
Returns: 10.0
7
0
0
1
0
Returns: 7.0
4
9
6
8
8
Returns: 5.196354571557175
4
1
10
3
5
Returns: 4.553285259815844
3
7
1
6
9
Returns: 3.7078984587219317
4
10
4
3
8
Returns: 4.924524740498578
4
5
8
9
0
Returns: 4.680895267085048
5
4
9
6
4
Returns: 6.488535126184254
7
8
3
9
1
Returns: 8.245075183695173
7
3
6
7
4
Returns: 9.439795965854538
6
8
2
7
0
Returns: 6.687840680529059
3
8
10
1
1
Returns: 4.854158551930306
4
8
9
8
7
Returns: 5.228677567248667
5
5
0
9
4
Returns: 6.3832568134466605
7
9
8
10
7
Returns: 9.76811635255514
7
10
5
0
9
Returns: 8.443774350734289
3
3
5
6
2
Returns: 3.6124679165830487
3
7
3
2
1
Returns: 3.920003384726102
3
9
5
8
6
Returns: 3.711703534171559
2
4
7
0
10
Returns: 2.1711057304277643
2
6
1
5
0
Returns: 2.11231884057971
4
0
2
2
8
Returns: 5.0752121287277525
7
4
7
0
0
Returns: 19.82927822797933
7
7
8
9
7
Returns: 9.809569701609018
7
1
2
3
4
Returns: 10.307598813453723