-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path874_Walking-Robot-Simulation.cpp
75 lines (65 loc) · 1.93 KB
/
874_Walking-Robot-Simulation.cpp
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
class Solution {
public:
void dfsHelper(vector<int>& commands, set<pair<int,int>>& obstacles_set, int idx, pair<int,int>location, int flow, int& maxDist){
// base case
if(idx == commands.size()) return;
// Moving foward
if(commands[idx] > 0){
int steps = commands[idx];
int ori_x = location.first;
int ori_y = location.second;
int move_x = 0;
int move_y = 0;
switch(flow){
case 0:
move_y = 1;
break;
case 1:
move_x = 1;
break;
case 2:
move_y = -1;
break;
case 3:
move_x = -1;
break;
}
while(steps != 0){
if(obstacles_set.count(make_pair(ori_x+move_x, ori_y+move_y))) break;
ori_x += move_x;
ori_y += move_y;
--steps;
}
maxDist = max(maxDist, ori_x*ori_x + ori_y * ori_y);
dfsHelper(commands, obstacles_set, idx+1, make_pair(ori_x, ori_y), flow, maxDist);
return;
}
// Toward right
if(commands[idx] == -1){
int newflow = (flow == 3) ? 0 : flow+1;
dfsHelper(commands, obstacles_set, idx+1, location, newflow, maxDist);
return;
}
// Toward left
if(commands[idx] == -2){
int newflow = (flow == 0) ? 3 : flow-1;
dfsHelper(commands, obstacles_set, idx+1, location, newflow, maxDist);
return;
}
}
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
pair<int,int> location;
//0: North
//1: East
//2: South
//3: West
int flow = 0;
int maxDist = -1;
set<pair<int,int>> obstacles_set;
for(auto& p : obstacles){
obstacles_set.insert(make_pair(p[0], p[1]));
}
dfsHelper(commands, obstacles_set, 0, location, flow, maxDist);
return maxDist;
}
};