-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
109 lines (101 loc) · 2.47 KB
/
Solution.cs
File metadata and controls
109 lines (101 loc) · 2.47 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
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
public class Robot
{
int n, m, x, y, st, round;
int[][] dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
public Robot(int width, int height)
{
n = width;
m = height;
x = 0;
y = 0;
st = 1;
round = 2 * (n + m) - 4; // remove 4 duplicate [0,0], [0,n], [0,m], [n,m]
// 0 : North, 1: East, 2: South, 3: West
}
public void Step(int num)
{
while (num > 0)
{
num -= ((num - 1) / round) * round;
// k * dirs[st][0] + x = n
// k * dirs[st][1] + y = m
int k = 0;
if (st == 0)
{
k = m - y - 1;
}
else if (st == 1)
{
k = n - x - 1;
}
else if (st == 2)
{
k = y;
}
else
{
k = x;
}
if (k == 0)
{
st = (st - 1 + 4) % 4;
}
else
{
k = Math.Min(k, num);
num -= k;
x += k * dirs[st][0];
y += k * dirs[st][1];
}
}
}
public int[] GetPos()
{
return [x, y];
}
public string GetDir()
{
if (st == 0) return "North";
if (st == 1) return "East";
if (st == 2) return "South";
return "West";
}
}
/**
* Your Robot object will be instantiated and called as such:
* Robot obj = new Robot(width, height);
* obj.Step(num);
* int[] param_2 = obj.GetPos();
* string param_3 = obj.GetDir();
*/
public class Solution
{
public List<dynamic> Execute(string[] actions, int[][] values)
{
List<dynamic> result = [];
Robot robot = null;
for (int i = 0; i < actions.Length; i++)
{
switch (actions[i])
{
case "Robot":
robot = new Robot(values[i][0], values[i][1]);
result.Add(null);
break;
case "step":
robot.Step(values[i][0]);
result.Add(null);
break;
case "getPos":
result.Add(robot.GetPos());
break;
case "getDir":
result.Add(robot.GetDir());
break;
default:
break;
}
}
return result;
}
}