-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathCrazyBot.java
More file actions
147 lines (111 loc) · 3.15 KB
/
CrazyBot.java
File metadata and controls
147 lines (111 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// BEGIN CUT HERE
/*
// PROBLEM STATEMENT
// An out-of-control robot is placed on a plane, and it takes n random steps. At each step, the robot chooses one of four directions randomly and moves one unit in that direction. The probabilities of the robot choosing north, south, east or west are north, south, east and west percent, respectively.
The robot's path is considered simple if it never visits the same point more than once. (The robot's starting point is always the first visited point.) Return a double containing the probability that the robot's path is simple. For example, "EENE" and "ENW" are simple, but "ENWS" and "WWWWSNE" are not ('E', 'W', 'N' and 'S' represent east, west, north and south, respectively).
DEFINITION
Class:CrazyBot
Method:getProbability
Parameters:int, int, int, int, int
Returns:double
Method signature:double getProbability(int n, int east, int west, int south, int north)
NOTES
-Your return must have relative or absolute error less than 1E-9.
CONSTRAINTS
-n will be between 1 and 14, inclusive.
-east will be between 0 and 100, inclusive.
-west will be between 0 and 100, inclusive.
-south will be between 0 and 100, inclusive.
-north will be between 0 and 100, inclusive.
-The sum of east, west, south and north will be 100.
EXAMPLES
0)
1
25
25
25
25
Returns: 1.0
The robot only takes one step, so it never visits a point more than once.
1)
2
25
25
25
25
Returns: 0.75
The robot will visit its starting point twice only if the two moves are in opposite directions.
2)
7
50
0
0
50
Returns: 1.0
Here, the only possible directions are north and east, so the robot will never visit the same point twice.
3)
14
50
50
0
0
Returns: 1.220703125E-4
Here, the only possible directions are east and west. The only two available paths are "EEEEEEEEEEEEEE" and "WWWWWWWWWWWWWW". The probability is equal to 2 / (2^14).
4)
14
25
25
25
25
Returns: 0.008845493197441101
The probability is quite small for n=14.
NNNEESWSESWW
NWWWNSNEEE
int begin = Math.max(p1.x, p2.x);
int end = Math.min(p1.x, p2.x);
line |= Math.pow(2, begin) - Math.pow(2, end);
p(N, n, e, s, w) = sum(i=14, 12, .., 2, n*p(N-i, n, e, s, w) : )
*/
// END CUT HERE
import java.util.*;
public class CrazyBot
{
boolean[][] v;
double n, s, e, w;
double res;
private void visit(int i, double p, int x, int y)
{
if (v[x][y])
return;
if (i == 0)
{
res += p;
return;
}
v[x][y] = true;
visit(i-1, p * n, x, y+1);
visit(i-1, p * s, x, y-1);
visit(i-1, p * e, x+1, y);
visit(i-1, p * w, x-1, y);
v[x][y] = false;
}
public double getProbability(int T, int E, int W, int S, int N)
{
n = (double)N / 100;
s = (double)S / 100;
e = (double)E / 100;
w = (double)W / 100;
res = 0.0;
v = new boolean[50][50];
for (int i = 0; i < 50; ++i)
for (int j = 0; j < 50; ++j)
v[j][i] = false;
visit(T, 1, 20, 20);
return res;
}
public static void main(String[] args)
{
CrazyBot temp = new CrazyBot();
System.out.println(temp.getProbability(14, 25, 25, 25, 25));
}
}