{"id":2944,"date":"2018-06-27T22:26:01","date_gmt":"2018-06-28T05:26:01","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2944"},"modified":"2018-06-27T22:59:44","modified_gmt":"2018-06-28T05:59:44","slug":"leetcode-289-game-of-life","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-289-game-of-life\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 289. Game of Life"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 289. Game of Life - \u5237\u9898\u627e\u5de5\u4f5c EP199\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/juGxbF-eadU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>According to the\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Conway%27s_Game_of_Life\" target=\"_blank\" rel=\"noopener\">Wikipedia&#8217;s article<\/a>: &#8220;The\u00a0<b>Game of Life<\/b>, also known simply as\u00a0<b>Life<\/b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&#8221;<\/p>\n<p>Given a\u00a0<i>board<\/i>\u00a0with\u00a0<i>m<\/i>\u00a0by\u00a0<i>n<\/i>\u00a0cells, each cell has an initial state\u00a0<i>live<\/i>\u00a0(1) or\u00a0<i>dead<\/i>\u00a0(0). Each cell interacts with its\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Moore_neighborhood\" target=\"_blank\" rel=\"noopener\">eight neighbors<\/a>\u00a0(horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):<\/p>\n<ol>\n<li>Any live cell with fewer than two live neighbors dies, as if caused by under-population.<\/li>\n<li>Any live cell with two or three live neighbors lives on to the next generation.<\/li>\n<li>Any live cell with more than three live neighbors dies, as if by over-population..<\/li>\n<li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.<\/li>\n<\/ol>\n<p>Write a function to compute the next state (after one update) of the board given its current state.\u00a0The next state is created by applying the above rules simultaneously to every cell in the current state, where\u00a0births and deaths occur simultaneously.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: \r\n<\/strong><span id=\"example-input-1-1\">[\r\n\u00a0 [0,1,0],\r\n\u00a0 [0,0,1],\r\n\u00a0 [1,1,1],\r\n\u00a0 [0,0,0]\r\n]<\/span>\r\n<strong>Output: \r\n<\/strong><span id=\"example-output-1\">[\r\n\u00a0 [0,0,0],\r\n\u00a0 [1,0,1],\r\n\u00a0 [0,1,1],\r\n\u00a0 [0,1,0]\r\n]<\/span>\r\n<\/pre>\n<p><b>Follow up<\/b>:<\/p>\n<ol>\n<li>Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.<\/li>\n<li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?<\/li>\n<\/ol>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2947\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/289-ep199.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/289-ep199.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/289-ep199-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/289-ep199-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 5 ms\r\nclass Solution {\r\npublic:\r\n  void gameOfLife(vector&lt;vector&lt;int&gt;&gt;&amp; board) {\r\n    int m = board.size();\r\n    int n = m ? board[0].size() : 0;\r\n    for (int i = 0; i &lt; m; ++i)\r\n      for (int j = 0; j &lt; n; ++j) {\r\n        int lives = 0;\r\n        \/\/ Scan the 3x3 region including (j, i).\r\n        for (int y = max(0, i - 1); y &lt; min(m, i + 2); ++y)\r\n          for (int x = max(0, j - 1); x &lt; min(n, j + 2); ++x)\r\n            lives += board[y][x] &amp; 1;\r\n        if (lives == 3 || lives - board[i][j] == 3) board[i][j] |= 0b10;\r\n      }\r\n    for (int i = 0; i &lt; m; ++i)\r\n      for (int j = 0; j &lt; n; ++j) \r\n        board[i][j] &gt;&gt;= 1;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem According to the\u00a0Wikipedia&#8217;s article: &#8220;The\u00a0Game of Life, also known simply as\u00a0Life, is a cellular automaton devised by the British mathematician John Horton Conway in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126,48],"tags":[16,273,177,179],"class_list":["post-2944","post","type-post","status-publish","format-standard","hentry","category-bit","category-simulation","tag-bit","tag-in-place","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2944","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=2944"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2944\/revisions"}],"predecessor-version":[{"id":2948,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2944\/revisions\/2948"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}