-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution874.cs
58 lines (52 loc) · 1.75 KB
/
Solution874.cs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution874
{
/// <summary>
/// 874. Walking Robot Simulation - Medium
/// <a href="https://leetcode.com/problems/walking-robot-simulation">See the problem</a>
/// </summary>
public int RobotSim(int[] commands, int[][] obstacles)
{
// Directions: North, East, South, West
int[][] directions = [
[0, 1], // North
[1, 0], // East
[0, -1], // South
[-1, 0] // West
];
// Convert obstacles to a set for quick lookup
var obstacleSet = new HashSet<(int, int)>();
foreach (var obstacle in obstacles) {
obstacleSet.Add((obstacle[0], obstacle[1]));
}
// Initialize the robot's position and direction
int x = 0, y = 0, dir = 0;
int maxDistance = 0;
foreach (var command in commands) {
if (command == -2) {
// Turn left
dir = (dir + 3) % 4;
} else if (command == -1) {
// Turn right
dir = (dir + 1) % 4;
} else {
// Move forward
int dx = directions[dir][0];
int dy = directions[dir][1];
for (int step = 0; step < command; step++) {
int nx = x + dx;
int ny = y + dy;
if (obstacleSet.Contains((nx, ny))) {
break; // Stop moving if there's an obstacle
}
x = nx;
y = ny;
maxDistance = Math.Max(maxDistance, x * x + y * y);
}
}
}
return maxDistance;
}
}