-
[LeetCode] 1041. Robot Bounded In CircleLeetCode 2022. 1. 9. 20:04728x90
https://leetcode.com/problems/robot-bounded-in-circle/
Robot Bounded In Circle - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Robot Bounded In Circle - Medium

로봇이 주어진 지시대로 움직인다.
G 면 현재 방향으로 1칸, L이면 왼쪽으로 꺾고, R이면 오른쪽으로 꺾는다.
이 지시를 무한번 반복할때, 이 로봇이 같은 경로를 반복해서 움직이는지 판단하는 문제다.
한 싸이클의 지시가 주어졌을때, 방향이 바뀌어 있다면, 이 로봇은 어떻게든 돌고 돌아 기존 경로로 돌아오게 된다.
왜냐하면 움직이는 거리가 늘어나지 않고 일정할테니 말이다.
만약 방향이 바뀌어 있지 않다면, (북쪽이라면) 0칸을 움직이지 않고는 위로 쭉 갈 수밖에 없다.
그러므로 한 싸이클을 돈 상태에서 로봇의 방향과 위치를 파악해주면 된다.
전체 풀이 코드
class Solution { public: bool isRobotBounded(string instructions) { int x=0, y=0, move[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}}, dir = 0; for (char c:instructions) { if (c=='G') x+=move[dir][0], y+=move[dir][1]; if (c=='L') dir = (dir+1)%4; if (c=='R') dir = (dir+7)%4; } return dir || (x==0 && y==0); } };'LeetCode' 카테고리의 다른 글
[LeetCode] 452. Minimum Number of Arrows to Burst Balloons (0) 2022.01.13 [LeetCode] 701. Insert into a Binary Search Tree (0) 2022.01.12 [LeetCode] 1094. Car Pooling (0) 2022.01.06 [LeetCode] 1026. Maximum Difference Between Node and Ancestor (0) 2021.12.31 [LeetCode] 1015. Smallest Integer Divisible by K (0) 2021.12.30