-
Notifications
You must be signed in to change notification settings - Fork 3
/
874.cpp
34 lines (34 loc) · 1.24 KB
/
874.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
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
int x = 0;
int y = 0;
int directionIdx = 0;
int res = 0;
unordered_set<int> obstacleHash;
for (auto& obstacle : obstacles) {
obstacle[0] += 30000;
obstacle[1] += 30000;
obstacleHash.insert((obstacle[0] << 16) + obstacle[1]);
}
for (auto& command : commands) {
if (command == -2) {
directionIdx = (directionIdx - 1 + 4) % 4;
}
else if (command == -1) {
directionIdx = (directionIdx + 1) % 4;
}
else {
for (int i = 0; i < command; ++i) {
int next = ((x + directions[directionIdx].first + 30000) << 16) + (y + directions[directionIdx].second + 30000);
if (obstacleHash.find(next) != obstacleHash.end()) break;
x += directions[directionIdx].first;
y += directions[directionIdx].second;
res = max(res, x * x + y * y);
}
}
}
return res;
}
};