{"id":9710,"date":"2022-05-01T22:37:13","date_gmt":"2022-05-02T05:37:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9710"},"modified":"2022-05-01T22:44:46","modified_gmt":"2022-05-02T05:44:46","slug":"leetcode-2257-count-unguarded-cells-in-the-grid","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2257-count-unguarded-cells-in-the-grid\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2257. Count Unguarded Cells in the Grid"},"content":{"rendered":"\n<p>You are given two integers&nbsp;<code>m<\/code>&nbsp;and&nbsp;<code>n<\/code>&nbsp;representing a&nbsp;<strong>0-indexed<\/strong>&nbsp;<code>m x n<\/code>&nbsp;grid. You are also given two 2D integer arrays&nbsp;<code>guards<\/code>&nbsp;and&nbsp;<code>walls<\/code>&nbsp;where&nbsp;<code>guards[i] = [row<sub>i<\/sub>, col<sub>i<\/sub>]<\/code>&nbsp;and&nbsp;<code>walls[j] = [row<sub>j<\/sub>, col<sub>j<\/sub>]<\/code>&nbsp;represent the positions of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;guard and&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;wall respectively.<\/p>\n\n\n\n<p>A guard can see&nbsp;<strong>every<\/strong>&nbsp;cell in the four cardinal directions (north, east, south, or west) starting from their position unless&nbsp;<strong>obstructed<\/strong>&nbsp;by a wall or another guard. A cell is&nbsp;<strong>guarded<\/strong>&nbsp;if there is&nbsp;<strong>at least<\/strong>&nbsp;one guard that can see it.<\/p>\n\n\n\n<p>Return<em>&nbsp;the number of unoccupied cells that are&nbsp;<strong>not<\/strong>&nbsp;<strong>guarded<\/strong>.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/03\/10\/example1drawio2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> The guarded and unguarded cells are shown in red and green respectively in the above diagram.\nThere are a total of 7 unguarded cells, so we return 7.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/03\/10\/example2drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The unguarded cells are shown in green in the above diagram.\nThere are a total of 4 unguarded cells, so we return 4.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= m, n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>2 &lt;= m * n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= guards.length, walls.length &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li><code>2 &lt;= guards.length + walls.length &lt;= m * n<\/code><\/li><li><code>guards[i].length == walls[j].length == 2<\/code><\/li><li><code>0 &lt;= row<sub>i<\/sub>, row<sub>j<\/sub>&nbsp;&lt; m<\/code><\/li><li><code>0 &lt;= col<sub>i<\/sub>, col<sub>j<\/sub>&nbsp;&lt; n<\/code><\/li><li>All the positions in&nbsp;<code>guards<\/code>&nbsp;and&nbsp;<code>walls<\/code>&nbsp;are&nbsp;<strong>unique<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Enumerate each cell, for each guard, shoot rays to 4 directions, mark cells on the way to 1 and stop when hit a guard or a wall. <\/p>\n\n\n\n<p>Time complexity: O(mn)<br>Space complexity: O(mn)<\/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  int countUnguarded(int m, int n, vector<vector<int>>& guards, vector<vector<int>>& walls) {\n    vector<vector<int>> s(m, vector<int>(n));\n    for (const auto& g : guards)\n      s[g[0]][g[1]] = 2;\n    \n    for (const auto& w : walls)\n      s[w[0]][w[1]] = 3;\n    \n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j) {\n        if (s[i][j] != 2) continue;\n        for (int y = i - 1; y >= 0; s[y--][j] = 1)\n          if (s[y][j] >= 2) break;          \n        for (int y = i + 1; y < m; s[y++][j] = 1)\n          if (s[y][j] >= 2) break;        \n        for (int x = j - 1; x >= 0; s[i][x--] = 1)\n          if (s[i][x] >= 2) break;    \n        for (int x = j + 1; x < n; s[i][x++] = 1)\n          if (s[i][x] >= 2) break;          \n      }    \n    int ans = 0;\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j)\n        ans += !s[i][j];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two integers&nbsp;m&nbsp;and&nbsp;n&nbsp;representing a&nbsp;0-indexed&nbsp;m x n&nbsp;grid. You are also given two 2D integer arrays&nbsp;guards&nbsp;and&nbsp;walls&nbsp;where&nbsp;guards[i] = [rowi, coli]&nbsp;and&nbsp;walls[j] = [rowj, colj]&nbsp;represent the positions 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":[48],"tags":[278,177,179],"class_list":["post-9710","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-grid","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9710","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=9710"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9710\/revisions"}],"predecessor-version":[{"id":9713,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9710\/revisions\/9713"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}