class Solution(object):
def isRobotBounded(self, instructions):
"""
:type instructions: str
:rtype: bool
"""
cnt = 0
class Robot:
def __init__(self):
self.x = 0
self.y = 0
self.Dir = "N"
def turnLeft(self, Dir):
if Dir == "N":
self.Dir = "W"
elif Dir == "W":
self.Dir = "S"
elif Dir == "S":
self.Dir = "E"
else:
self.Dir = "N"
def turnRight(self, Dir):
if Dir == "N":
self.Dir = "E"
elif Dir == "W":
self.Dir = "N"
elif Dir == "S":
self.Dir = "W"
else:
self.Dir = "S"
def GoForward(self, x, y, Dir):
if Dir == "N":
self.y += 1
elif Dir == "W":
self.x -= 1
elif Dir == "S":
self.y -= 1
else:
self.x += 1
# 해당 robot을 선언한다
robot = Robot()
# 최대 경우의 수 4번까지를 돌린다
while cnt < 4:
for x in instructions:
if x == "G":
robot.GoForward(robot.x, robot.y, robot.Dir)
elif x == "L":
robot.turnLeft(robot.Dir)
else :
robot.turnRight(robot.Dir)
cnt += 1
if ( robot.x == 0 and robot.y == 0):
return True
else :
return False
왜 최대 경우 수 4번까지 일까 ?
Input: "GL"
Output: true
Explanation:
The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ....
이 경우, 1칸 씩 이동, 총 4번 반복하면, 제자리에 오고,
4번 이후 부터는 다시 시작하는 개념이기 때문이다