{"id":3257,"date":"2018-07-22T03:57:19","date_gmt":"2018-07-22T10:57:19","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3257"},"modified":"2018-07-22T03:57:46","modified_gmt":"2018-07-22T10:57:46","slug":"leetcode-874-walking-robot-simulation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-874-walking-robot-simulation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 874. Walking Robot Simulation"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>A robot on an infinite grid starts at point (0, 0) and faces north.\u00a0 The robot can receive one of three possible types of commands:<\/p>\n<ul>\n<li><code>-2<\/code>: turn left 90 degrees<\/li>\n<li><code>-1<\/code>: turn right 90 degrees<\/li>\n<li><code>1 &lt;= x &lt;= 9<\/code>: move forward\u00a0<code>x<\/code>\u00a0units<\/li>\n<\/ul>\n<p>Some of the grid squares are obstacles.<\/p>\n<p>The\u00a0<code>i<\/code>-th obstacle is at grid point\u00a0<code>(obstacles[i][0], obstacles[i][1])<\/code><\/p>\n<p>If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)<\/p>\n<p>Return the\u00a0<strong>square<\/strong>\u00a0of the maximum Euclidean distance that the robot will be from the origin.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>commands = <span id=\"example-input-1-1\">[4,-1,3]<\/span>, obstacles = <span id=\"example-input-1-2\">[]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">25<\/span>\r\nExplanation: robot will go to (3, 4)\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>commands = <span id=\"example-input-2-1\">[4,-1,4,-2,4]<\/span>, obstacles = <span id=\"example-input-2-2\">[[2,4]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">65<\/span>\r\n<strong>Explanation<\/strong>: robot will be stuck at (1, 4) before turning left and going to (1, 8)<\/pre>\n<\/div>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>0 &lt;= commands.length &lt;= 10000<\/code><\/li>\n<li><code>0 &lt;= obstacles.length &lt;= 10000<\/code><\/li>\n<li><code>-30000 &lt;= obstacle[i][0] &lt;= 30000<\/code><\/li>\n<li><code>-30000 &lt;= obstacle[i][1] &lt;= 30000<\/code><\/li>\n<li>The answer is guaranteed to be less than\u00a0<code>2 ^ 31<\/code>.<\/li>\n<\/ol>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Time complexity: O(n + sum(x)) = O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 60 ms\r\nclass Solution {\r\npublic:\r\n  int robotSim(vector&lt;int&gt;&amp; commands, vector&lt;vector&lt;int&gt;&gt;&amp; obstacles) {\r\n    int x = 0;\r\n    int y = 0;\r\n    int dirs[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; \/\/ WNES\r\n    int d = 1;\r\n    unordered_map&lt;int, unordered_set&lt;int&gt;&gt; o;\r\n    for (const auto obstacle : obstacles)\r\n      o[obstacle[0]].insert(obstacle[1]);\r\n    int ans = 0;\r\n    for (int c : commands) {      \r\n      if (c == -2)\r\n        d = (d - 1 + 4) % 4;\r\n      else if (c == -1)\r\n        d = (d + 1) % 4;\r\n      else {\r\n        while (c--) {\r\n          int tx = x + dirs[d][0];\r\n          int ty = y + dirs[d][1];\r\n          if (o.count(tx) &amp;&amp; o.at(tx).count(ty))\r\n            break;\r\n          x = tx;\r\n          y = ty;\r\n          ans = max(ans, x * x + y * y);          \r\n        }\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem A robot on an infinite grid starts at point (0, 0) and faces north.\u00a0 The robot can receive one of three possible types of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,48],"tags":[222,82,179],"class_list":["post-3257","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-simulation","tag-easy","tag-hashtable","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3257","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=3257"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3257\/revisions"}],"predecessor-version":[{"id":3259,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3257\/revisions\/3259"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}