|
| 1 | +<h3 align="left"> 1368. Minimum Cost to Make at Least One Valid Path in a Grid</h3> |
| 2 | +<div><p>Given an <code>m x n</code> grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of <code>grid[i][j]</code> can be:</p> |
| 3 | + |
| 4 | +<ul> |
| 5 | + <li><code>1</code> which means go to the cell to the right. (i.e go from <code>grid[i][j]</code> to <code>grid[i][j + 1]</code>)</li> |
| 6 | + <li><code>2</code> which means go to the cell to the left. (i.e go from <code>grid[i][j]</code> to <code>grid[i][j - 1]</code>)</li> |
| 7 | + <li><code>3</code> which means go to the lower cell. (i.e go from <code>grid[i][j]</code> to <code>grid[i + 1][j]</code>)</li> |
| 8 | + <li><code>4</code> which means go to the upper cell. (i.e go from <code>grid[i][j]</code> to <code>grid[i - 1][j]</code>)</li> |
| 9 | +</ul> |
| 10 | + |
| 11 | +<p>Notice that there could be some signs on the cells of the grid that point outside the grid.</p> |
| 12 | + |
| 13 | +<p>You will initially start at the upper left cell <code>(0, 0)</code>. A valid path in the grid is a path that starts from the upper left cell <code>(0, 0)</code> and ends at the bottom-right cell <code>(m - 1, n - 1)</code> following the signs on the grid. The valid path does not have to be the shortest.</p> |
| 14 | + |
| 15 | +<p>You can modify the sign on a cell with <code>cost = 1</code>. You can modify the sign on a cell <strong>one time only</strong>.</p> |
| 16 | + |
| 17 | +<p>Return <em>the minimum cost to make the grid have at least one valid path</em>.</p> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong>Example 1:</strong></p> |
| 21 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid1.png" style="width: 400px; height: 390px;"> |
| 22 | +<pre><strong>Input:</strong> grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]] |
| 23 | +<strong>Output:</strong> 3 |
| 24 | +<strong>Explanation:</strong> You will start at point (0, 0). |
| 25 | +The path to (3, 3) is as follows. (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) change the arrow to down with cost = 1 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) change the arrow to down with cost = 1 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) change the arrow to down with cost = 1 --> (3, 3) |
| 26 | +The total cost = 3. |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p><strong>Example 2:</strong></p> |
| 30 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid2.png" style="width: 350px; height: 341px;"> |
| 31 | +<pre><strong>Input:</strong> grid = [[1,1,3],[3,2,2],[1,1,4]] |
| 32 | +<strong>Output:</strong> 0 |
| 33 | +<strong>Explanation:</strong> You can follow the path from (0, 0) to (2, 2). |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong>Example 3:</strong></p> |
| 37 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid3.png" style="width: 200px; height: 192px;"> |
| 38 | +<pre><strong>Input:</strong> grid = [[1,2],[4,3]] |
| 39 | +<strong>Output:</strong> 1 |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>m == grid.length</code></li> |
| 47 | + <li><code>n == grid[i].length</code></li> |
| 48 | + <li><code>1 <= m, n <= 100</code></li> |
| 49 | + <li><code>1 <= grid[i][j] <= 4</code></li> |
| 50 | +</ul> |
| 51 | +</div> |
0 commit comments