-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalkingRobotSimulation.cpp
More file actions
33 lines (32 loc) · 1.04 KB
/
walkingRobotSimulation.cpp
File metadata and controls
33 lines (32 loc) · 1.04 KB
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
// Source: https://leetcode.com/problems/walking-robot-simulation/
// Author: Miao Zhang
// Date: 2021-03-20
class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
int i = 0;
int j = 0;
vector<vector<int>> dirs{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int direction = 1;
unordered_map<int, unordered_set<int>> obs;
for (auto o: obstacles) obs[o[0]].insert(o[1]);
int res = 0;
for (int c: commands) {
if (c == -2) {
direction = (direction - 1 + 4) % 4;
} else if (c == -1) {
direction = (direction + 1) % 4;
} else {
while (c--) {
int x = i + dirs[direction][0];
int y = j + dirs[direction][1];
if (obs.count(x) && obs.at(x).count(y)) break;
i = x;
j = y;
res = max(res, x * x + y * y);
}
}
}
return res;
}
};