-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode2069.cpp
More file actions
69 lines (49 loc) · 1.11 KB
/
leetcode2069.cpp
File metadata and controls
69 lines (49 loc) · 1.11 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
class Robot {
public:
int w,h;
int x, y;
int dir;
vector<int> dx = {1,0,-1,0};
vector<int> dy = {0,1,0,-1};
vector<string> dirs = {"East","North","West","South"};
int perimeter ;
Robot(int width, int height) {
w=width;
h=height;
x = 0;
y=0;
dir =0;
perimeter = 2*(w+h)-4;
}
void step(int num) {
num = num%perimeter;
if(num == 0 && x == 0 && y == 0){
dir=3;
return ;
}
while(num--){
int nx = x+dx[dir];
int ny = y+dy[dir];
if(nx<0 || nx>=w || ny <0 || ny>= h){
dir = (dir+1)%4;
nx = x+dx[dir];
ny = y+dy[dir];
}
x=nx;
y=ny;
}
}
vector<int> getPos() {
return {x,y};
}
string getDir() {
return dirs[dir];
}
};
/**
* Your Robot object will be instantiated and called as such:
* Robot* obj = new Robot(width, height);
* obj->step(num);
* vector<int> param_2 = obj->getPos();
* string param_3 = obj->getDir();
*/