LeetCode1041. Robot Bounded In Circle

一、题目

On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that:

The north direction is the positive direction of the y-axis.
The south direction is the negative direction of the y-axis.
The east direction is the positive direction of the x-axis.
The west direction is the negative direction of the x-axis.
The robot can receive one of three instructions:

“G”: go straight 1 unit.
“L”: turn 90 degrees to the left (i.e., anti-clockwise direction).
“R”: turn 90 degrees to the right (i.e., clockwise direction).
The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

Example 1:

Input: instructions = “GGLLGG”
Output: true
Explanation: The robot is initially at (0, 0) facing the north direction.
“G”: move one step. Position: (0, 1). Direction: North.
“G”: move one step. Position: (0, 2). Direction: North.
“L”: turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.
“L”: turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.
“G”: move one step. Position: (0, 1). Direction: South.
“G”: move one step. Position: (0, 0). Direction: South.
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0).
Based on that, we return true.
Example 2:

Input: instructions = “GG”
Output: false
Explanation: The robot is initially at (0, 0) facing the north direction.
“G”: move one step. Position: (0, 1). Direction: North.
“G”: move one step. Position: (0, 2). Direction: North.
Repeating the instructions, keeps advancing in the north direction and does not go into cycles.
Based on that, we return false.
Example 3:

Input: instructions = “GL”
Output: true
Explanation: The robot is initially at (0, 0) facing the north direction.
“G”: move one step. Position: (0, 1). Direction: North.
“L”: turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.
“G”: move one step. Position: (-1, 1). Direction: West.
“L”: turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.
“G”: move one step. Position: (-1, 0). Direction: South.
“L”: turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.
“G”: move one step. Position: (0, 0). Direction: East.
“L”: turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0).
Based on that, we return true.

Constraints:

1 <= instructions.length <= 100
instructions[i] is ‘G’, ‘L’ or, ‘R’.

二、题解

class Solution {
public:
    bool isRobotBounded(string instructions) {
        int n = instructions.size();
        vector<vector<int>> dir = {{0,1},{1,0},{0,-1},{-1,0}};
        int direction = 0;
        int x = 0,y = 0;
        for(int i = 0;i < n;i++){
            if(instructions[i] == 'G'){
                x += dir[direction][0];
                y += dir[direction][1];
            }
            else if(instructions[i] == 'L'){
                direction += 3;
                direction %= 4;
            }
            else{
                direction++;
                direction %= 4;
            }
        }
        return direction != 0 || (x == 0 && y == 0);
    }
};
  • 22
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值