> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/1041.robot_bounded_in_circle.md).

# 1041.Robot Bounded in Circle

**1041.Robot Bounded in Circle**

难度:Easy

> 在无限的平面上，机器人最初位于 (0, 0) 处，面朝北方。机器人可以接受下列三条指令之一：

"G"：直走 1 个单位 "L"：左转 90 度 "R"：右转 90 度 机器人按顺序执行指令 instructions，并一直重复它们。

只有在平面中存在环使得机器人永远无法离开时，返回 true。否则，返回 false。

```
示例 1：

输入："GGLLGG"
输出：true
解释：
机器人从 (0,0) 移动到 (0,2)，转 180 度，然后回到 (0,0)。
重复这些指令，机器人将保持在以原点为中心，2 为半径的环中进行移动。
示例 2：

输入："GG"
输出：false
解释：
机器人无限向北移动。
示例 3：

输入："GL"
输出：true
解释：
机器人按 (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ... 进行移动。
 

提示：

1 <= instructions.length <= 100
instructions[i] 在 {'G', 'L', 'R'} 中
```

整体方向只能是上下左右，则最多四次，如果不能回到原点，则必定发散。

```
class Solution {
public:
    bool isRobotBounded(string instructions) {
        int direc=0;
        int flag=4;
        pair<int,int>pos(0,0);
        set<pair<int,int>> tmp;
   
        while(flag--){
        pair<int,int>t=pos;
             
        for(auto c: instructions)
        {
            
// 0,4,-4 stands for UP, 1，-3 stands for Right, -1,3 stands for Left, 2 and -2 stands for Down;
            if(c=='L')
                direc=(direc-1)%4;
            else if(c=='R')
                direc = (direc+1)%4;
            else 
            {
                
                pos.second +=(direc==0) - (direc ==2 || direc ==-2);
                pos.first += (direc==1)+(direc==-3) -(direc ==-1)-(direc==3);
          //   cout<<pos.first<<" "<<pos.second<<endl;  
            }                     
        }
//   cout<<direc<<endl;  
            if( pos.first==0 && pos.second ==0) return true;
}
        //  cout<<direc<<" "<<pos.first<<" "<<pos.second<<endl;
        return false;
    }
};
```
