-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrobot_bounded_in_circle.cpp
66 lines (61 loc) · 1.81 KB
/
robot_bounded_in_circle.cpp
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
class Solution {
enum Directions{ NORTH, SOUTH, EAST, WEST };
public:
bool isRobotBounded(string instructions) {
if(instructions.size() == 0) return false;
int x = 0, y = 0;
Directions d = NORTH;
for(auto &instruction : instructions) {
if(instruction == 'G') {
switch(d) {
case NORTH:
y++;
break;
case SOUTH:
y--;
break;
case WEST:
x--;
break;
case EAST:
x++;
break;
}
}
else if(instruction == 'L') {
switch(d) {
case NORTH:
d = WEST;
break;
case SOUTH:
d = EAST;
break;
case EAST:
d = NORTH;
break;
case WEST:
d = SOUTH;
break;
}
}
else {
switch(d) {
case NORTH:
d = EAST;
break;
case SOUTH:
d = WEST;
break;
case EAST:
d = SOUTH;
break;
case WEST:
d = NORTH;
break;
}
}
}
if((x == 0 and y == 0) or !(d == NORTH)) return true;
return false;
}
};