{"id":5180,"date":"2019-05-14T09:38:33","date_gmt":"2019-05-14T16:38:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5180"},"modified":"2019-05-15T21:38:07","modified_gmt":"2019-05-16T04:38:07","slug":"leetcode-1041-robot-bounded-in-circle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1041-robot-bounded-in-circle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1041. Robot Bounded In Circle"},"content":{"rendered":"\n<p>On an infinite plane, a&nbsp;robot initially stands at&nbsp;<code>(0, 0)<\/code>&nbsp;and faces north.&nbsp;&nbsp;The robot can receive one of three instructions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>\"G\"<\/code>: go straight 1 unit;<\/li><li><code>\"L\"<\/code>: turn 90 degrees to the left;<\/li><li><code>\"R\"<\/code>: turn 90 degress to the right.<\/li><\/ul>\n\n\n\n<p>The robot performs the&nbsp;<code>instructions<\/code>&nbsp;given in order, and repeats them forever.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;if and only if there exists a circle in the plane such that the robot never leaves the circle.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>\"GGLLGG\"\n<strong>Output: <\/strong>true\n<strong>Explanation: <\/strong>\nThe robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).\nWhen repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>\"GG\"\n<strong>Output: <\/strong>false\n<strong>Explanation: <\/strong>\nThe robot moves north indefinitely.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>\"GL\"\n<strong>Output: <\/strong>true\n<strong>Explanation: <\/strong>\nThe robot moves from (0, 0) -&gt; (0, 1) -&gt; (-1, 1) -&gt; (-1, 0) -&gt; (0, 0) -&gt; ...\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= instructions.length &lt;= 100<\/code><\/li><li><code>instructions[i]<\/code>&nbsp;is in&nbsp;<code>{'G', 'L', 'R'}<\/code><\/li><\/ol>\n\n\n\n<p><strong>Solution: Simulation<\/strong><\/p>\n\n\n\n<p>When instructions end, if the robot is back to (0,0) or is not facing north (which guarantees it will come back to 0, 0 for another 1 or 3 rounds)<br><\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isRobotBounded(string instructions) {    \n    int x = 0;\n    int y = 0;\n    int d = 0;\n    vector<int> dx{0, 1, 0, -1};\n    vector<int> dy{-1, 0, 1, 0};    \n    for (char c : instructions) {\n      if (c == 'G') {\n        x += dx[d];\n        y += dy[d];\n      } else {\n        d = (d + (c == 'L' ? 3 : 1)) % 4;\n      }\n    }\n    return x == 0 && y == 0 || d;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>On an infinite plane, a&nbsp;robot initially stands at&nbsp;(0, 0)&nbsp;and faces north.&nbsp;&nbsp;The robot can receive one of three instructions: &#8220;G&#8221;: go straight 1 unit; &#8220;L&#8221;: turn&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,179],"class_list":["post-5180","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5180"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5180\/revisions"}],"predecessor-version":[{"id":5196,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5180\/revisions\/5196"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}