1
class Solution {
2
public:
3
// N--> left(-2):W, right(-1):E
4
// S--> left:E, right:W
5
// E--> left:N, right:S
6
// W--> left:S, right:N
7

8
vector<vector<int>> change = { // for direction change
9
{3, 2},
10
{2, 3},
11
{0, 1},
12
{1, 0}};
13

14
vector<vector<int>> sign = {
15
// signs for x and y coordinates movement
16
{0, 1}, // North
17
{0, -1}, // south
18
{1, 0}, // east
19
{-1, 0} // west
20
};
21

22
int robotSim(vector<int> &commands, vector<vector<int>> &obstacles) {
23
set<vector<int>> s;
24
for (int i = 0; i < obstacles.size(); i++) {
25
s.insert(obstacles[i]);
26
}
27
int direction = 0;
28
int ans = 0;
29
int xi = 0, yi = 0;
30
for (int i = 0; i < commands.size(); i++) {
31
if (commands[i] <= 9 && commands[i] >= 1) {
32
int signx = sign[direction][0];
33
int signy = sign[direction][1];
34
while (commands[i]) {
35
xi += signx;
36
yi += signy;
37
if (s.count({xi, yi})) {
38
xi -= signx;
39
yi -= signy;
40
break;
41
}
42
commands[i]--;
43
}
44
} else {
45
direction = change[direction][commands[i] + 2];
46
}
47
ans = max(ans, xi * xi + yi * yi);
48
}
49
return ans;
50
}
51
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0