forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
75 lines (70 loc) · 2.04 KB
/
Solution.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:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
set<pair<int, int>> s ;
for (auto o: obstacles)
s.insert(pair<int, int>(o[0], o[1])) ;
int dir = 0 ;
int x = 0, y = 0 ;
int m = 0 ;
for (auto c: commands)
{
//cout << c << ":" << x << ' ' << y << endl ;
long long d = x*x + y*y ;
if (m < d)
m = d ;
if (-2 == c)
{
dir += 3 ;
}
else if (-1 == c)
{
++dir ;
}
else
{
int step = c ;
dir %= 4 ;
if (0 == dir)
{
while (step--)
{
if (s.find(pair<int, int>(x, y+1)) != s.end())
break ;
//cout << "++i" << endl ;
++y ;
}
}
else if (1 == dir)
{
while (step--)
{
if (s.find(pair<int, int>(x+1, y)) != s.end())
break ;
++x ;
}
}
else if (2 == dir)
{
while (step--)
{
if (s.find(pair<int, int>(x, y-1)) != s.end())
break ;
--y ;
}
}
else if (3 == dir)
{
while (step--)
{
if (s.find(pair<int, int>(x-1, y)) != s.end())
break ;
--x ;
}
}
}
}
//cout << ":" << x << ' ' << y << endl ;
return max(m, x*x + y*y);
}
};