Skip to content

Commit 90ebdd0

Browse files
authored
feat: add solutions to lc problem: No.0874
No.0874.Walking Robot Simulation
1 parent 9d0680d commit 90ebdd0

File tree

7 files changed

+341
-66
lines changed

7 files changed

+341
-66
lines changed

solution/0800-0899/0872.Leaf-Similar Trees/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
深度优先搜索。
52+
**方法一:DFS**
53+
54+
后序遍历。
5355

5456
<!-- tabs:start -->
5557

solution/0800-0899/0874.Walking Robot Simulation/README.md

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,142 @@
8484

8585
<!-- 这里可写通用的实现逻辑 -->
8686

87+
**方法一:模拟**
88+
8789
<!-- tabs:start -->
8890

8991
### **Python3**
9092

9193
<!-- 这里可写当前语言的特殊实现逻辑 -->
9294

9395
```python
94-
96+
class Solution:
97+
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
98+
dirs = [[-1, 0], [0, 1], [1, 0], [0, -1]]
99+
s = {(x, y) for x, y in obstacles}
100+
ans, p = 0, 1
101+
x = y = 0
102+
for v in commands:
103+
if v == -2:
104+
p = (p + 3) % 4
105+
elif v == -1:
106+
p = (p + 1) % 4
107+
else:
108+
for _ in range(v):
109+
nx, ny = x + dirs[p][0], y + dirs[p][1]
110+
if (nx, ny) in s:
111+
break
112+
x, y = nx, ny
113+
ans = max(ans, x * x + y * y)
114+
return ans
95115
```
96116

97117
### **Java**
98118

99119
<!-- 这里可写当前语言的特殊实现逻辑 -->
100120

101121
```java
122+
class Solution {
123+
public int robotSim(int[] commands, int[][] obstacles) {
124+
int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
125+
Set<String> s = new HashSet<>();
126+
for (int[] v : obstacles) {
127+
s.add(v[0] + "." + v[1]);
128+
}
129+
int ans = 0, p = 1;
130+
int x = 0, y = 0;
131+
for (int v : commands) {
132+
if (v == -2) {
133+
p = (p + 3) % 4;
134+
} else if (v == -1) {
135+
p = (p + 1) % 4;
136+
} else {
137+
while (v-- > 0) {
138+
int nx = x + dirs[p][0], ny = y + dirs[p][1];
139+
if (s.contains(nx + "." + ny)) {
140+
break;
141+
}
142+
x = nx;
143+
y = ny;
144+
ans = Math.max(ans, x * x + y * y);
145+
}
146+
}
147+
}
148+
return ans;
149+
}
150+
}
151+
```
152+
153+
### **C++**
154+
155+
```cpp
156+
class Solution {
157+
public:
158+
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
159+
vector<vector<int>> dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
160+
unordered_set<string> s;
161+
for (auto v : obstacles) s.insert(to_string(v[0]) + "." + to_string(v[1]));
162+
int ans = 0, p = 1;
163+
int x = 0, y = 0;
164+
for (int v : commands)
165+
{
166+
if (v == -2) p = (p + 3) % 4;
167+
else if (v == -1) p = (p + 1) % 4;
168+
else
169+
{
170+
while (v--)
171+
{
172+
int nx = x + dirs[p][0], ny = y + dirs[p][1];
173+
if (s.count(to_string(nx) + "." + to_string(ny))) break;
174+
x = nx;
175+
y = ny;
176+
ans = max(ans, x * x + y * y);
177+
}
178+
}
179+
}
180+
return ans;
181+
}
182+
};
183+
```
102184
185+
### **Go**
186+
187+
```go
188+
func robotSim(commands []int, obstacles [][]int) int {
189+
dirs := [][]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}
190+
s := map[string]bool{}
191+
for _, v := range obstacles {
192+
t := strconv.Itoa(v[0]) + "." + strconv.Itoa(v[1])
193+
s[t] = true
194+
}
195+
ans, p := 0, 1
196+
x, y := 0, 0
197+
for _, v := range commands {
198+
if v == -2 {
199+
p = (p + 3) % 4
200+
} else if v == -1 {
201+
p = (p + 1) % 4
202+
} else {
203+
for i := 0; i < v; i++ {
204+
nx, ny := x+dirs[p][0], y+dirs[p][1]
205+
t := strconv.Itoa(nx) + "." + strconv.Itoa(ny)
206+
if s[t] {
207+
break
208+
}
209+
x, y = nx, ny
210+
ans = max(ans, x*x+y*y)
211+
}
212+
}
213+
}
214+
return ans
215+
}
216+
217+
func max(a, b int) int {
218+
if a > b {
219+
return a
220+
}
221+
return b
222+
}
103223
```
104224

105225
### **...**

solution/0800-0899/0874.Walking Robot Simulation/README_EN.md

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,131 @@ The furthest point the robot ever gets from the origin is (0, 6), which squared
8383
### **Python3**
8484

8585
```python
86-
86+
class Solution:
87+
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
88+
dirs = [[-1, 0], [0, 1], [1, 0], [0, -1]]
89+
s = {(x, y) for x, y in obstacles}
90+
ans, p = 0, 1
91+
x = y = 0
92+
for v in commands:
93+
if v == -2:
94+
p = (p + 3) % 4
95+
elif v == -1:
96+
p = (p + 1) % 4
97+
else:
98+
for _ in range(v):
99+
nx, ny = x + dirs[p][0], y + dirs[p][1]
100+
if (nx, ny) in s:
101+
break
102+
x, y = nx, ny
103+
ans = max(ans, x * x + y * y)
104+
return ans
87105
```
88106

89107
### **Java**
90108

91109
```java
110+
class Solution {
111+
public int robotSim(int[] commands, int[][] obstacles) {
112+
int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
113+
Set<String> s = new HashSet<>();
114+
for (int[] v : obstacles) {
115+
s.add(v[0] + "." + v[1]);
116+
}
117+
int ans = 0, p = 1;
118+
int x = 0, y = 0;
119+
for (int v : commands) {
120+
if (v == -2) {
121+
p = (p + 3) % 4;
122+
} else if (v == -1) {
123+
p = (p + 1) % 4;
124+
} else {
125+
while (v-- > 0) {
126+
int nx = x + dirs[p][0], ny = y + dirs[p][1];
127+
if (s.contains(nx + "." + ny)) {
128+
break;
129+
}
130+
x = nx;
131+
y = ny;
132+
ans = Math.max(ans, x * x + y * y);
133+
}
134+
}
135+
}
136+
return ans;
137+
}
138+
}
139+
```
140+
141+
### **C++**
142+
143+
```cpp
144+
class Solution {
145+
public:
146+
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
147+
vector<vector<int>> dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
148+
unordered_set<string> s;
149+
for (auto v : obstacles) s.insert(to_string(v[0]) + "." + to_string(v[1]));
150+
int ans = 0, p = 1;
151+
int x = 0, y = 0;
152+
for (int v : commands)
153+
{
154+
if (v == -2) p = (p + 3) % 4;
155+
else if (v == -1) p = (p + 1) % 4;
156+
else
157+
{
158+
while (v--)
159+
{
160+
int nx = x + dirs[p][0], ny = y + dirs[p][1];
161+
if (s.count(to_string(nx) + "." + to_string(ny))) break;
162+
x = nx;
163+
y = ny;
164+
ans = max(ans, x * x + y * y);
165+
}
166+
}
167+
}
168+
return ans;
169+
}
170+
};
171+
```
92172
173+
### **Go**
174+
175+
```go
176+
func robotSim(commands []int, obstacles [][]int) int {
177+
dirs := [][]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}
178+
s := map[string]bool{}
179+
for _, v := range obstacles {
180+
t := strconv.Itoa(v[0]) + "." + strconv.Itoa(v[1])
181+
s[t] = true
182+
}
183+
ans, p := 0, 1
184+
x, y := 0, 0
185+
for _, v := range commands {
186+
if v == -2 {
187+
p = (p + 3) % 4
188+
} else if v == -1 {
189+
p = (p + 1) % 4
190+
} else {
191+
for i := 0; i < v; i++ {
192+
nx, ny := x+dirs[p][0], y+dirs[p][1]
193+
t := strconv.Itoa(nx) + "." + strconv.Itoa(ny)
194+
if s[t] {
195+
break
196+
}
197+
x, y = nx, ny
198+
ans = max(ans, x*x+y*y)
199+
}
200+
}
201+
}
202+
return ans
203+
}
204+
205+
func max(a, b int) int {
206+
if a > b {
207+
return a
208+
}
209+
return b
210+
}
93211
```
94212

95213
### **...**
Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,27 @@
11
class Solution {
22
public:
33
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
4-
set<pair<int, int>> s ;
5-
for (auto o: obstacles)
6-
s.insert(pair<int, int>(o[0], o[1])) ;
7-
8-
int dir = 0 ;
9-
int x = 0, y = 0 ;
10-
int m = 0 ;
11-
12-
for (auto c: commands)
4+
vector<vector<int>> dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
5+
unordered_set<string> s;
6+
for (auto v : obstacles) s.insert(to_string(v[0]) + "." + to_string(v[1]));
7+
int ans = 0, p = 1;
8+
int x = 0, y = 0;
9+
for (int v : commands)
1310
{
14-
//cout << c << ":" << x << ' ' << y << endl ;
15-
long long d = x*x + y*y ;
16-
if (m < d)
17-
m = d ;
18-
if (-2 == c)
19-
{
20-
dir += 3 ;
21-
}
22-
else if (-1 == c)
23-
{
24-
++dir ;
25-
}
11+
if (v == -2) p = (p + 3) % 4;
12+
else if (v == -1) p = (p + 1) % 4;
2613
else
2714
{
28-
int step = c ;
29-
dir %= 4 ;
30-
if (0 == dir)
31-
{
32-
while (step--)
33-
{
34-
if (s.find(pair<int, int>(x, y+1)) != s.end())
35-
break ;
36-
//cout << "++i" << endl ;
37-
++y ;
38-
}
39-
40-
}
41-
else if (1 == dir)
42-
{
43-
while (step--)
44-
{
45-
if (s.find(pair<int, int>(x+1, y)) != s.end())
46-
break ;
47-
++x ;
48-
}
49-
}
50-
else if (2 == dir)
51-
{
52-
while (step--)
53-
{
54-
if (s.find(pair<int, int>(x, y-1)) != s.end())
55-
break ;
56-
--y ;
57-
}
58-
}
59-
else if (3 == dir)
15+
while (v--)
6016
{
61-
while (step--)
62-
{
63-
if (s.find(pair<int, int>(x-1, y)) != s.end())
64-
break ;
65-
--x ;
66-
}
17+
int nx = x + dirs[p][0], ny = y + dirs[p][1];
18+
if (s.count(to_string(nx) + "." + to_string(ny))) break;
19+
x = nx;
20+
y = ny;
21+
ans = max(ans, x * x + y * y);
6722
}
6823
}
6924
}
70-
71-
//cout << ":" << x << ' ' << y << endl ;
72-
73-
return max(m, x*x + y*y);
25+
return ans;
7426
}
7527
};

0 commit comments

Comments
 (0)