Skip to content

Commit 261d716

Browse files
Create robot_bounded_in_circle.cpp
1 parent a6001d3 commit 261d716

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

robot_bounded_in_circle.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Solution {
2+
enum Directions{ NORTH, SOUTH, EAST, WEST };
3+
public:
4+
bool isRobotBounded(string instructions) {
5+
6+
if(instructions.size() == 0) return false;
7+
int x = 0, y = 0;
8+
9+
Directions d = NORTH;
10+
11+
for(auto &instruction : instructions) {
12+
if(instruction == 'G') {
13+
14+
switch(d) {
15+
case NORTH:
16+
y++;
17+
break;
18+
case SOUTH:
19+
y--;
20+
break;
21+
case WEST:
22+
x--;
23+
break;
24+
case EAST:
25+
x++;
26+
break;
27+
}
28+
}
29+
else if(instruction == 'L') {
30+
switch(d) {
31+
case NORTH:
32+
d = WEST;
33+
break;
34+
case SOUTH:
35+
d = EAST;
36+
break;
37+
case EAST:
38+
d = NORTH;
39+
break;
40+
case WEST:
41+
d = SOUTH;
42+
break;
43+
}
44+
}
45+
else {
46+
switch(d) {
47+
case NORTH:
48+
d = EAST;
49+
break;
50+
case SOUTH:
51+
d = WEST;
52+
break;
53+
case EAST:
54+
d = SOUTH;
55+
break;
56+
case WEST:
57+
d = NORTH;
58+
break;
59+
}
60+
}
61+
}
62+
if((x == 0 and y == 0) or !(d == NORTH)) return true;
63+
64+
return false;
65+
}
66+
};

0 commit comments

Comments
 (0)