{"id":6526,"date":"2020-03-22T03:46:31","date_gmt":"2020-03-22T10:46:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6526"},"modified":"2020-03-22T03:58:06","modified_gmt":"2020-03-22T10:58:06","slug":"leetcode-1391-check-if-there-is-a-valid-path-in-a-grid","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1391-check-if-there-is-a-valid-path-in-a-grid\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1391. Check if There is a Valid Path in a Grid"},"content":{"rendered":"\n<p>Given a&nbsp;<em>m<\/em>&nbsp;x&nbsp;<em>n<\/em><code>grid<\/code>. Each cell of the&nbsp;<code>grid<\/code>&nbsp;represents a street. The street of&nbsp;<code>grid[i][j]<\/code>&nbsp;can be:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>1<\/strong>&nbsp;which means a street connecting the left cell and the right cell.<\/li><li><strong>2<\/strong>&nbsp;which means a street connecting the upper cell and the lower cell.<\/li><li><strong>3<\/strong>&nbsp;which means a street connecting the left cell and the lower cell.<\/li><li><strong>4<\/strong>&nbsp;which means a street connecting the right cell and the lower cell.<\/li><li><strong>5<\/strong>&nbsp;which means a street connecting the left cell and the upper cell.<\/li><li><strong>6<\/strong>&nbsp;which means a street connecting the right cell and the upper cell.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/03\/05\/main.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>You will initially start at the street of the&nbsp;upper-left cell&nbsp;<code>(0,0)<\/code>. A valid path in the grid is a path which starts from the upper left&nbsp;cell&nbsp;<code>(0,0)<\/code>&nbsp;and ends at the bottom-right&nbsp;cell&nbsp;<code>(m - 1, n - 1)<\/code>.&nbsp;<strong>The path should only follow the streets<\/strong>.<\/p>\n\n\n\n<p><strong>Notice<\/strong>&nbsp;that you are&nbsp;<strong>not allowed<\/strong>&nbsp;to change any street.<\/p>\n\n\n\n<p>Return&nbsp;<em>true<\/em>&nbsp;if there is a valid path in the grid or&nbsp;<em>false<\/em>&nbsp;otherwise.<\/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\/2020\/03\/05\/e1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[2,4,3],[6,5,2]]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> As shown you can start at cell (0, 0) and visit all the cells of the grid to reach (m - 1, n - 1).\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\/2020\/03\/05\/e2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,2,1],[1,2,1]]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> As shown you the street at cell (0, 0) is not connected with any street of any other cell and you will get stuck at cell (0, 0)\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> grid = [[1,1,2]]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> You will get stuck at cell (0, 1) and you cannot reach cell (0, 2).\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,1,1,1,1,1,3]]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[2],[2],[2],[2],[2],[2],[6]]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == grid.length<\/code><\/li><li><code>n == grid[i].length<\/code><\/li><li><code>1 &lt;= m, n &lt;= 300<\/code><\/li><li><code>1 &lt;= grid[i][j] &lt;= 6<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: BFS<\/strong><\/h2>\n\n\n\n<p>Need to check both sides (x, y) -&gt; (tx, ty) and (tx, ty) -&gt; (x, y) to make sure a path exist.<\/p>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(m*n)<\/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 hasValidPath(vector<vector<int>>& grid) {\n    const int m = grid.size();\n    const int n = grid[0].size();\n    vector<int> dirs{0, -1, 0, 1, 0}; \/\/ [up, left, down, right]    \n    vector<set<int>> rules{\n      {1, 3}, {0, 2}, {1, 2}, \n      {2, 3}, {0, 1}, {0, 3}};\n    queue<int> q{{0}};\n    vector<int> seen(m * n);\n    seen[0] = 1;\n    while (!q.empty()) {\n      int cx = q.front() % n;\n      int cy = q.front() \/ n;      \n      q.pop();\n      if (cx == n - 1 && cy == m - 1) return true;      \n      for (int d : rules[grid[cy][cx] - 1]) {\n        int x = cx + dirs[d];\n        int y = cy + dirs[d + 1];\n        int key = y * n + x;\n        if (x < 0 || x >= n || y < 0 || y >= m || seen[key] \n           || !rules[grid[y][x] - 1].count((d + 2) % 4)) continue;\n        seen[key] = 1;\n        q.push(key);\n      }\n    }\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;m&nbsp;x&nbsp;ngrid. Each cell of the&nbsp;grid&nbsp;represents a street. The street of&nbsp;grid[i][j]&nbsp;can be: 1&nbsp;which means a street connecting the left cell and the right cell. 2&nbsp;which&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[34,278,177,366,42],"class_list":["post-6526","post","type-post","status-publish","format-standard","hentry","category-searching","tag-bfs","tag-grid","tag-medium","tag-omn","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6526","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=6526"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6526\/revisions"}],"predecessor-version":[{"id":6529,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6526\/revisions\/6529"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}