-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1041.cpp
More file actions
91 lines (90 loc) · 1.94 KB
/
1041.cpp
File metadata and controls
91 lines (90 loc) · 1.94 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
#include <bits/stdc++.h>
using namespace std;
void changeDirection(int &directionX, int &directionY, char c, int ¤tX, int ¤tY)
{
if (c == 'L')
{
if (directionX == 0)
{
if (directionY == 1)
{
directionX = -1;
directionY = 0;
}
else
{
directionX = 1;
directionY = 0;
}
}
else
{
if (directionX == 1)
{
directionX = 0;
directionY = 1;
}
else
{
directionX = 0;
directionY = -1;
}
}
}
else if (c == 'R')
{
if (directionX == 0)
{
if (directionY == 1)
{
directionX = 1;
directionY = 0;
}
else
{
directionX = -1;
directionY = 0;
}
}
else
{
if (directionX == 1)
{
directionX = 0;
directionY = -1;
}
else
{
directionX = 0;
directionY = 1;
}
}
}
else
{
currentX += directionX;
currentY += directionY;
}
}
bool isRobotBounded(string instructions)
{
int directionX = 0, directionY = 1;
int currentX = 0, currentY = 0;
for (auto i = 0; i < 100; i++)
{
for (auto c : instructions)
{
changeDirection(directionX, directionY, c, currentX, currentY);
}
//cout << currentX << " " << currentY << " " << directionX << " " << directionY << endl;
if (currentX == 0 && currentY == 0)
{
return true;
}
}
return false;
}
int main()
{
cout << isRobotBounded("RLLGGLRGLGLLLGRLRLRLRRRRLRLGRLLLGGL");
}