Leetcode-5055 Robot Bounded In Circle(困于环中的机器人)

 1 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 2 int dx[] = {-1,0,1,0};
 3 int dy[] = {0,1,0,-1};
 4 class Solution
 5 {
 6     public:
 7         bool isRobotBounded(string instructions)
 8         {
 9             int fang = 0;
10             int x = 0,y = 0;
11             int T = 200;
12             while(T--)
13             {
14             _for(i,0,instructions.size())
15             {
16                 if(instructions[i]==‘G‘)
17                 {
18                     x += dx[fang];
19                     y += dy[fang];
20                 }
21                 else if(instructions[i]==‘L‘)
22                 {
23                     fang --;
24                     if(fang==-1)
25                         fang = 3;
26                 }
27                 else
28                 {
29                     fang ++;
30                     if(fang==4)
31                         fang = 0;
32                 }
33             }
34                 if(x==0 && y==0)
35                     break;
36             }
37             return x==0 && y==0;
38         }
39 };

数据范围小于100,那多循环几次看看是否形成闭合回路就行了

原文地址:https://www.cnblogs.com/Asurudo/p/10851865.html

时间: 2024-11-23 10:43:58

Leetcode-5055 Robot Bounded In Circle(困于环中的机器人)的相关文章

leetcode 1041. 困于环中的机器人

题目地址 : https://leetcode-cn.com/problems/robot-bounded-in-circle/ 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以接受下列三条指令之一: "G":直走 1 个单位"L":左转 90 度"R":右转 90 度机器人按顺序执行指令 instructions,并一直重复它们. 只有在平面中存在环使得机器人永远无法离开时,返回 true.否则,返回 false. 示例

[Swift]LeetCode1041. 困于环中的机器人 | Robot Bounded In Circle

On an infinite plane, a robot initially stands at (0, 0) and faces north.  The robot can receive one of three instructions: "G": go straight 1 unit; "L": turn 90 degrees to the left; "R": turn 90 degress to the right. The rob

leetcode 874 Robot Simulation

leetcode 874 Robot Simulation 限制:32MB, 1s 描述 A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands: -2: turn left 90 degrees -1: turn right 90 degrees 1 <= x <= 9: move forw

[LeetCode] Walking Robot Simulation 走路机器人仿真

A robot on an infinite grid starts at point (0, 0) and faces north.? The robot can receive one of three possible types of commands: -2: turn left 90 degrees -1: turn right 90 degrees 1 <= x <= 9: move forward?x?units Some of the grid squares are obs

LeetCode 1188. Design Bounded Blocking Queue

原题链接在这里:https://leetcode.com/problems/design-bounded-blocking-queue/ 题目: Implement a thread safe bounded blocking queue that has the following methods: BoundedBlockingQueue(int capacity) The constructor initializes the queue with a maximum capacity.

[LeetCode] 489. Robot Room Cleaner 扫地机器人

Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees. When it tries to move into a blocked cel

[Leetcode]657. Robot Return to Origin

Easy There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves. The move sequence is represented by a string, and the character moves[i]

算法--leetcode 657. Judge Route Circle

题目: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place. The move sequence is represented by a string. And each move is represent by a char

LeetCode第136场周赛

菜鸡我只做出了一道... 5055. 困于环中的机器人 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以接受下列三条指令之一: "G":直走 1 个单位 "L":左转 90 度 "R":右转 90 度 机器人按顺序执行指令 instructions,并一直重复它们. 只有在平面中存在环使得机器人永远无法离开时,返回 true.否则,返回 false. 提示: 1 <= instructions.length <=