2577. Minimum Time to Visit a Cell In a Grid Hard
1/**
2 * [2577] Minimum Time to Visit a Cell In a Grid
3 *
4 * You are given a m x n matrix grid consisting of non-negative integers where grid[row][col] represents the minimum time required to be able to visit the cell (row, col), which means you can visit the cell (row, col) only when the time you visit it is greater than or equal to grid[row][col].
5 * You are standing in the top-left cell of the matrix in the 0^th second, and you must move to any adjacent cell in the four directions: up, down, left, and right. Each move you make takes 1 second.
6 * Return the minimum time required in which you can visit the bottom-right cell of the matrix. If you cannot visit the bottom-right cell, then return -1.
7 *
8 * <strong class="example">Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2023/02/14/yetgriddrawio-8.png" />
10 *
11 * Input: grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]]
12 * Output: 7
13 * Explanation: One of the paths that we can take is the following:
14 * - at t = 0, we are on the cell (0,0).
15 * - at t = 1, we move to the cell (0,1). It is possible because grid[0][1] <= 1.
16 * - at t = 2, we move to the cell (1,1). It is possible because grid[1][1] <= 2.
17 * - at t = 3, we move to the cell (1,2). It is possible because grid[1][2] <= 3.
18 * - at t = 4, we move to the cell (1,1). It is possible because grid[1][1] <= 4.
19 * - at t = 5, we move to the cell (1,2). It is possible because grid[1][2] <= 5.
20 * - at t = 6, we move to the cell (1,3). It is possible because grid[1][3] <= 6.
21 * - at t = 7, we move to the cell (2,3). It is possible because grid[2][3] <= 7.
22 * The final time is 7. It can be shown that it is the minimum time possible.
23 *
24 * <strong class="example">Example 2:
25 * <img alt="" src="https://assets.leetcode.com/uploads/2023/02/14/yetgriddrawio-9.png" style="width: 151px; height: 151px;" />
26 *
27 * Input: grid = [[0,2,4],[3,2,1],[1,0,4]]
28 * Output: -1
29 * Explanation: There is no path from the top left to the bottom-right cell.
30 *
31 *
32 * Constraints:
33 *
34 * m == grid.length
35 * n == grid[i].length
36 * 2 <= m, n <= 1000
37 * 4 <= m * n <= 10^5
38 * 0 <= grid[i][j] <= 10^5
39 * grid[0][0] == 0
40 *
41 *
42 * <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
43 * }
44 * .spoiler {overflow:hidden;}
45 * .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
46 * .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
47 * .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
48 * </style>
49 *
50 */
51pub struct Solution {}
52
53// problem: https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/
54// discuss: https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/discuss/?currentPage=1&orderBy=most_votes&query=
55
56// submission codes start here
57
58impl Solution {
59 pub fn minimum_time(grid: Vec<Vec<i32>>) -> i32 {
60 0
61 }
62}
63
64// submission codes end
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_2577() {
72 }
73}
74
Back
© 2025 bowen.ge All Rights Reserved.