-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathWalking Robot Simulation.cpp
54 lines (50 loc) · 1.44 KB
/
Walking Robot Simulation.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
// Runtime: 386 ms (Top 8.00%) | Memory: 45.9 MB (Top 26.80%)
class Solution {
public:
//N--> left(-2):W, right(-1):E
//S--> left:E, right:W
//E--> left:N, right:S
//W--> left:S, right:N
vector<vector<int>> change= { //for direction change
{3,2},
{2,3},
{0,1},
{1,0}
};
vector<vector<int>> sign = { //signs for x and y coordinates movement
{0,1},//North
{0,-1},//south
{1,0},//east
{-1,0}//west
};
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
set<vector<int>> s;
for(int i = 0;i<obstacles.size();i++){
s.insert(obstacles[i]);
}
int direction = 0;
int ans = 0;
int xi = 0,yi = 0;
for(int i = 0;i<commands.size();i++){
if(commands[i]<=9 && commands[i]>=1){
int signx = sign[direction][0];
int signy = sign[direction][1];
while(commands[i]){
xi += signx;
yi += signy;
if(s.count({xi,yi})){
xi -= signx;
yi -= signy;
break;
}
commands[i]--;
}
}
else{
direction = change[direction][commands[i]+2];
}
ans = max(ans,xi*xi+yi*yi);
}
return ans;
}
};