From bb2a683a58c1564b411fe0ba9e858adf4d684525 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 17 Oct 2024 09:33:11 +0800 Subject: [PATCH] Add solution and test-cases for problem 1391 --- .../1.png | Bin 0 -> 20864 bytes .../README.md | 54 +++++++++ .../Solution.go | 112 +++++++++++++++++- .../Solution_test.go | 12 +- .../e1.png | Bin 0 -> 14654 bytes .../e2.png | Bin 0 -> 11298 bytes 6 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/1.png create mode 100644 leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/README.md create mode 100644 leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/e1.png create mode 100644 leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/e2.png diff --git a/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/1.png b/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/1.png new file mode 100644 index 0000000000000000000000000000000000000000..31d85c6d48034f253843d5cecdf51d14be0f11a7 GIT binary patch literal 20864 zcmeHvdpy)>-@mr)BV<#*pl!nZYm^Gr#M5%|wQK@BRIr`+4@hpI?7$`+^YE&h6Q4 zA|||4SV%}nY^SM_m5|V^ej%aR67#td0@2hc$Fprd9Umz3v>+vgR-!Tps21(dZ9skLPeiC;Kcffe<2--X(uIbE9$=zQ} zvpT&kK+^op?PK#*(&oe|Uw0o@s>)>c-$*~b?vve>Fr8=X(u#+aFW)z*Te#ayWR>w| z@~GaCtc;S#G1+uS@l)o#*`*OHKkn81a%I(=PJAdrpjDBp`cDf2UWt-ZA0D;}k;*0+ zHv8AyE5tZ1eZQvEGlJhkg)bKjz0&i)B?_7yheFXt$H_nA2ln)f+84HS8)G4FAj zSBpdCqM;8<>lY=h?^yOHhiGxXO!t~KZ|CtRsq@zx%sa;@jvPO7%$%jJgX z6@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`N1T_2r_X9dB|)s5H9 zERFA6ON$N$TMi6t+GfDL<_g)#a(y)1S_FIo%BpT$$M=TZA+ZLZ#D{ez2j1BkH z-eH%p)yir~3#)RN(cIZ@Jby{PlZzsF54~$u;_7w%2!=Vn(zpBNWqV{oa-A)Sls-EX z^~pllqQhU-zZuFJO$!V;e)qvyl5jJv$VidV=2D(S$P)wRqe z+k?hnGads>bjt?)==!`Zdda0Sv=g<7xUOLd&@ECa&@3^>b#KvS4)#&>}Rz zwJO=d8C%62sa90cy_U(8U=I<79$z19mGT?NI;@4}I8rbUtKtn#^~CBu=4KTJ&u$ly z4|sFr7oCA-E+zhH&`Q^FO$;u#)L9u*Dw8ASTU%@6!+Dc8Mv6@IwePn&;l>dnadzrI zjv4C!udQy{1m3vU>Uo-)RXF~Hh7dJOkkQU<9=(`L5i%eg{$+w-MWyyXXg|w;c@3mBqc4Ya=vz&>9 zROXO@gA%SMw(a>Id$0X5_w1j#2Z+;Cc5UD?Mk-@Dug=!ka@z*bsx0eVdVc{5X;xw8 z62@0{RTOl-W%yh!7)YKwIL-~YP#pAeT(l*LtH&f2{f0}6vInZ8o`f6ie>OZ8iTo+R ly8pO81=vO4j&rj_kG?&<;m}p^R}+PH8t*pB-hTMZ{{dD|UOfN+ literal 0 HcmV?d00001 diff --git a/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/README.md b/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/README.md new file mode 100644 index 000000000..73a6ca156 --- /dev/null +++ b/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/README.md @@ -0,0 +1,54 @@ +# [1391. Check if There is a Valid Path in a Grid][title] + +## Description +You are given an `m x n` `grid`. Each cell of `grid` represents a street. The street of `grid[i][j]` can be: + +- `1` which means a street connecting the left cell and the right cell. +- `2` which means a street connecting the upper cell and the lower cell. +- `3` which means a street connecting the left cell and the lower cell. +- `4` which means a street connecting the right cell and the lower cell. +- `5` which means a street connecting the left cell and the upper cell. +- `6` which means a street connecting the right cell and the upper cell. + +![1](./1.png) + +You will initially start at the street of the upper-left cell `(0, 0)`. A valid path in the grid is a path that starts from the upper left cell `(0, 0)` and ends at the bottom-right cell `(m - 1, n - 1)`. **The path should only follow the streets**. + +**Notice** that you are **not allowed** to change any street. + +Return `true` if there is a valid path in the grid or `false` otherwise. + +**Example 1:** + +![e1](./e1.png) + +``` +Input: grid = [[2,4,3],[6,5,2]] +Output: true +Explanation: As shown you can start at cell (0, 0) and visit all the cells of the grid to reach (m - 1, n - 1). +``` + +**Example 2:** + +![e2](./e1.png) + +``` +Input: grid = [[1,2,1],[1,2,1]] +Output: false +Explanation: 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) +``` + +**Example 3:** + +``` +Input: grid = [[1,1,2]] +Output: false +Explanation: You will get stuck at cell (0, 1) and you cannot reach cell (0, 2). +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/Solution.go b/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/Solution.go index d115ccf5e..4393a4a5c 100755 --- a/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/Solution.go +++ b/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/Solution.go @@ -1,5 +1,113 @@ package Solution -func Solution(x bool) bool { - return x +var dirFn = [6]func(int, int, [][]int) [][2]int{ + func(x, y int, grid [][]int) [][2]int { + m, n := len(grid), len(grid[0]) + res := make([][2]int, 0) + + nx, ny := x, y-1 + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 4 || grid[nx][ny] == 6) { + res = append(res, [2]int{nx, ny}) + } + nx, ny = x, y+1 + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 3 || grid[nx][ny] == 5) { + res = append(res, [2]int{nx, ny}) + } + return res + }, + func(x, y int, grid [][]int) [][2]int { + m, n := len(grid), len(grid[0]) + res := make([][2]int, 0) + + nx, ny := x-1, y + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 3 || grid[nx][ny] == 4) { + res = append(res, [2]int{nx, ny}) + } + nx, ny = x+1, y + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 5 || grid[nx][ny] == 6) { + res = append(res, [2]int{nx, ny}) + } + return res + }, + func(x, y int, grid [][]int) [][2]int { + m, n := len(grid), len(grid[0]) + res := make([][2]int, 0) + + nx, ny := x, y-1 + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 4 || grid[nx][ny] == 6) { + res = append(res, [2]int{nx, ny}) + } + nx, ny = x+1, y + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 5 || grid[nx][ny] == 6) { + res = append(res, [2]int{nx, ny}) + } + return res + }, + func(x, y int, grid [][]int) [][2]int { + m, n := len(grid), len(grid[0]) + res := make([][2]int, 0) + + nx, ny := x, y+1 + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 3 || grid[nx][ny] == 5) { + res = append(res, [2]int{nx, ny}) + } + nx, ny = x+1, y + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 5 || grid[nx][ny] == 6) { + res = append(res, [2]int{nx, ny}) + } + return res + }, + func(x, y int, grid [][]int) [][2]int { + m, n := len(grid), len(grid[0]) + res := make([][2]int, 0) + + nx, ny := x, y-1 + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 4 || grid[nx][ny] == 6) { + res = append(res, [2]int{nx, ny}) + } + nx, ny = x-1, y + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 3 || grid[nx][ny] == 4) { + res = append(res, [2]int{nx, ny}) + } + return res + }, + func(x, y int, grid [][]int) [][2]int { + m, n := len(grid), len(grid[0]) + res := make([][2]int, 0) + + nx, ny := x, y+1 + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 3 || grid[nx][ny] == 5) { + res = append(res, [2]int{nx, ny}) + } + nx, ny = x-1, y + if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 3 || grid[nx][ny] == 4) { + res = append(res, [2]int{nx, ny}) + } + return res + }, +} + +func Solution(grid [][]int) bool { + m, n := len(grid)-1, len(grid[0])-1 + q := [][2]int{{0, 0}} + v := make(map[[2]int]struct{}) + v[[2]int{0, 0}] = struct{}{} + for len(q) > 0 { + nq := make([][2]int, 0) + for _, pos := range q { + x, y := pos[0], pos[1] + if x == m && y == n { + return true + } + for _, next := range dirFn[grid[x][y]-1](x, y, grid) { + if _, ok := v[next]; ok { + continue + } + nq = append(nq, next) + v[next] = struct{}{} + } + } + q = nq + } + return false } diff --git a/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/Solution_test.go b/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/Solution_test.go index 14ff50eb4..cc517444d 100755 --- a/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/Solution_test.go +++ b/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs [][]int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{2, 4, 3}, {6, 5, 2}}, true}, + {"TestCase2", [][]int{{1, 2, 1}, {1, 2, 1}}, false}, + {"TestCase3", [][]int{{1, 1, 2}}, false}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/e1.png b/leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/e1.png new file mode 100644 index 0000000000000000000000000000000000000000..014fbcba1f2e57a7278ba2b20b4a7e16a79714fc GIT binary patch literal 14654 zcmeHOdpy)>+t*=MnMHD#*^F$56*3Nqrj#ib+a?hmmQZ6i%H$AXg(=Hm4EBB7EyXUA zEq2?|EK89l%&>>DbTX1KvuP?}GBGiD@85448guH|_j&g7yzkQ=`{`rnK3w;8U-$R= zUWZ?Xr-z%S#sUpBH8stRAAPu0O>O2qfhZ}t20-oL5aQx3pKlyuQ zt@pm$b^B(?x6eoMZchHCzqsH0rEqoKP!(2f-yXG^BOYTp6I7c9^s2nZz2M(xJ@og2 zsFBs56C2P$_rn(EfuAb>c|Y|fH};USY#P|8*u>2*=(H1H`8-Zl=}CC={)mp&>p`{+ zXm0GKzED^azQe9zyFoUPOlWjr^pxMiya`dkM4Nf}nTd}1H_R%Um#!?%{f79SVPxe#A+UYLld*MPNnqdl^w&@HwG}1e@*J%l=<}P-i3L`N??A>hU9}w=6kdH_mo}S2t%s0%K8?>I5eO*>SY_)xpHR5 zYB*rHg~CONA#AFgpYiK@_lxG@+q9YQ^r7(QbCB)xV62x0v`6Hz+XIU#>xT^7MQGya zCE>|LPxcXc%h~k)iF0h%zD7p5i-`HG0%rd+Z>M27C^&ZMVivv|Ip&jJmz=Sv>0v0p z{;BbZ*wILDuAZuykX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