-
Notifications
You must be signed in to change notification settings - Fork 0
/
1041.robot-bounded-in-circle.cpp
50 lines (48 loc) · 1.26 KB
/
1041.robot-bounded-in-circle.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
/*
* @lc app=leetcode id=1041 lang=cpp
*
* [1041] Robot Bounded In Circle
*/
// @lc code=start
class Solution
{
public:
bool isRobotBounded(string instructions)
{
// N:(0,1), S(0,-1), w(-1,0), E(1,0)
pair<int, int> dir = {0, 1}; //{x,y}
pair<int, int> pos = {0, 0}; // pos from origin
for (auto ch : instructions)
{
if (ch == 'G')
{
pos.first += dir.first;
pos.second += dir.second;
}
else if (ch == 'L')
{
auto prev = dir;
dir.first = -prev.second;
dir.second = prev.first;
}
else
{
auto prev = dir;
dir.first = prev.second;
dir.second = -prev.first;
}
} //for
// two cases that will form a cycle
// case 1: if the robot return back to origin
// case 2: if the robot has dir that is not point to north
if (pos.first == 0 && pos.second == 0)
return true;
else if (dir.first != 0 || dir.second != 1)
return true;
return false;
}
};
// @lc code=end
// (0,1) -> (-1, 0)
// (0,1) -> (1, 0)
//O(n), n is the size of string