# Leetcode 1041. Robot Bounded In Circle ###### tags: `Leetcode(C++)` 題目 : https://leetcode.com/problems/robot-bounded-in-circle/ 。 想法 : 很有意思的一題,可以歸納出以下結論 : 1. 如果最後面北,且不在原點一定會是FALSE,在原點就會是TRUE。 2. 剩餘方向就一定會是TRUE。 Why? 因為走四次後就一定會回到原點,自己走走看(?)畫個圖(?) 時間複雜度 : O(n)。 程式碼 : ``` class Solution { public: bool isRobotBounded(string instructions) { int l=instructions.size(), x=0, y=0, dir=0; for(int i=0 ; i<l ; i++){ if(instructions[i] == 'G'){ if(dir == 0) y++; if(dir == 1) x--; if(dir == 2) y--; if(dir == 3) x++; } if(instructions[i] == 'L'){ dir++; dir%=4; } if(instructions[i] == 'R'){ dir--; if(dir < 0) dir+=4; } } if(dir == 0){ if(x == 0 && y == 0) return true; return false; } return true; } }; ```