-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path874.cpp
More file actions
29 lines (29 loc) · 835 Bytes
/
Copy path874.cpp
File metadata and controls
29 lines (29 loc) · 835 Bytes
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
class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
set<pair<long long, long long>> os;
for(vector<int> i : obstacles){
os.insert({i[0], i[1]});
}
long long x = 0, y = 0, dx = 0, dy = 1, ans = 0;
for(int c : commands){
if(c == -1){
dx = -dx;
swap(dx, dy);
}else if(c == -2){
dy = -dy;
swap(dx, dy);
}else{
for(int i = 0; i < c; i++){
x += dx, y += dy;
if(os.count({x, y})){
x -= dx, y -= dy;
break;
}
}
ans = max(ans, x*x+y*y);
}
}
return ans;
}
};