-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBoundedRobot.cpp
62 lines (57 loc) · 1.62 KB
/
BoundedRobot.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
// Problem: https://leetcode.com/problems/robot-bounded-in-circle/
#include<string>
using namespace std;
class BoundedRobot {
public:
bool isRobotBounded(string instructions) {
int steps[4];
steps[0] = steps[1] = steps[2] = steps[3] = 0;
int num_l = 0;
int num_r = 0;
// 0 is N, 1 is E, 2 is W, 3 is S.
int dir = 0;
for (int index = 0; index < instructions.size(); ++index) {
char ins = instructions[index];
if (ins == 'L' || ins == 'R') {
dir = updateDir(ins, dir);
if (ins == 'L') ++num_l;
else ++num_r;
} else {
steps[dir]++;
}
}
// Finding the finding displacement.
int final_vertical = abs(steps[0] - steps[3]);
int final_sidewards = abs(steps[1] - steps[2]);
if ((final_vertical + final_sidewards) == 0) return true;
// There is a certain direction.
num_l = num_l % 4;
num_r = num_r % 4;
if (abs(num_l - num_r) != 0) return true;
return false;
}
private:
int updateDir(char ins, int dir) {
// 0 is North.
if (dir == 0) {
if (ins == 'L') return 2;
return 1;
}
// 1 is East.
if (dir == 1) {
if (ins == 'L') return 0;
return 3;
}
// 2 is West.
if (dir == 2) {
if (ins == 'L') return 3;
return 0;
}
// 3 is South.
if (dir == 3) {
if (ins == 'L') return 1;
return 2;
}
return -1;
}
};