Skip to content

Commit a75a805

Browse files
authored
Create 874. Walking Robot Simulation (#575)
2 parents 6c0fe15 + 6166333 commit a75a805

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

874. Walking Robot Simulation

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
class Solution {
3+
public:
4+
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
5+
6+
unordered_set<string> obs;
7+
for (auto& obstacle : obstacles) {
8+
obs.insert(to_string(obstacle[0]) + "," + to_string(obstacle[1]));
9+
}
10+
11+
int x = 0, y = 0, dx = 0, dy = 1;
12+
int ans = 0;
13+
14+
for (int c : commands) {
15+
if (c == -1) {
16+
int temp = dx;
17+
dx = dy; dy = -temp;
18+
} else if (c == -2) {
19+
int temp = dx;
20+
dx = -dy; dy = temp;
21+
} else {
22+
23+
for (int i = 0; i < c; ++i) {
24+
int xx = x + dx;
25+
int yy = y + dy;
26+
27+
if (obs.find(to_string(xx) + "," + to_string(yy)) != obs.end()) break;
28+
x = xx;
29+
y = yy;
30+
ans = max(ans, x * x + y * y);
31+
}
32+
}
33+
}
34+
35+
return ans;
36+
}
37+
};

0 commit comments

Comments
 (0)