[LeetCode]1368. 使网格图至少有一条有效路径的最小代价

30 篇文章 1 订阅
27 篇文章 4 订阅

题目

1368. 使网格图至少有一条有效路径的最小代价

1368. 使网格图至少有一条有效路径的最小代价
给你一个 m x n 的网格图 grid 。 grid 中每个格子都有一个数字,对应着从该格子出发下一步走的方向。 grid[i][j] 中的数字可能为以下几种情况:

1 ,下一步往右走,也就是你会从 grid[i][j] 走到 grid[i][j + 1]
2 ,下一步往左走,也就是你会从 grid[i][j] 走到 grid[i][j - 1]
3 ,下一步往下走,也就是你会从 grid[i][j] 走到 grid[i + 1][j]
4 ,下一步往上走,也就是你会从 grid[i][j] 走到 grid[i - 1][j]
注意网格图中可能会有 无效数字 ,因为它们可能指向 grid 以外的区域。

一开始,你会从最左上角的格子 (0,0) 出发。我们定义一条 有效路径 为从格子 (0,0) 出发,每一步都顺着数字对应方向走,最终在最右下角的格子 (m - 1, n - 1) 结束的路径。有效路径 不需要是最短路径 。

你可以花费 cost = 1 的代价修改一个格子中的数字,但每个格子中的数字 只能修改一次 。

请你返回让网格图至少有一条有效路径的最小代价。

 

示例 1:



输入:grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
输出:3
解释:你将从点 (0, 0) 出发。
到达 (3, 3) 的路径为: (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) 花费代价 cost = 1 使方向向下 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) 花费代价 cost = 1 使方向向下 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) 花费代价 cost = 1 使方向向下 --> (3, 3)
总花费为 cost = 3.
示例 2:



输入:grid = [[1,1,3],[3,2,2],[1,1,4]]
输出:0
解释:不修改任何数字你就可以从 (0, 0) 到达 (2, 2) 。
示例 3:



输入:grid = [[1,2],[4,3]]
输出:1
示例 4:

输入:grid = [[2,2,2],[2,2,2]]
输出:3
示例 5:

输入:grid = [[4]]
输出:0
 

提示:

m == grid.length
n == grid[i].length
1 <= m, n <= 100

解法

方法1:01广度优先搜索

int m, n;
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

public int minCost(int[][] grid) {
    m = grid.length;
    n = grid[0].length;
    int[] dist = new int[m * n];
    Arrays.fill(dist, Integer.MAX_VALUE);
    dist[0] = 0;
    boolean[] vis = new boolean[m * n];
    Deque<Integer> deque = new ArrayDeque<>();
    deque.offerFirst(0);
    while (!deque.isEmpty()) {
        int cur_pos = deque.pollFirst();
        if (vis[cur_pos]) {
            continue;
        }
        vis[cur_pos] = true;
        int x = cur_pos / n, y = cur_pos % n;
        for (int i = 0; i < 4; i++) {
            int nx = x + dirs[i][0], ny = y + dirs[i][1];
            int new_pos = nx * n + ny;
            //1:右 2:左 3:下 4:上
            //当前的[x,y]的值如果是符合1 2 3 4 的值,则不需要修改,也就是 0
            int new_dist = dist[cur_pos] + (grid[x][y] == i + 1 ? 0 : 1);
            if (nx < 0 || nx >= m || ny < 0 || ny >= n) {
                continue;
            }
            if (new_dist < dist[new_pos]) {
                dist[new_pos] = new_dist;
                //01广度优先搜索,将0的那个点加入到队首,1的那个点加入到对尾
                if (grid[x][y] == i + 1) {
                    deque.offerFirst(new_pos);
                } else {
                    deque.offerLast(new_pos);
                }
            }
        }
    }
    return dist[m * n - 1];
}
  • 另,不适用vis标记数组
    int m, n;
        int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

        public int minCost(int[][] grid) {
            m = grid.length;
            n = grid[0].length;
            int[] dist = new int[m * n];
            Arrays.fill(dist, Integer.MAX_VALUE);
            dist[0] = 0;
            Deque<Integer> deque = new ArrayDeque<>();
            deque.offerFirst(0);
            while (!deque.isEmpty()) {
                int cur_pos = deque.pollFirst();
                int x = cur_pos / n, y = cur_pos % n;
                for (int i = 0; i < 4; i++) {
                    int nx = x + dirs[i][0], ny = y + dirs[i][1];
                    int new_pos = nx * n + ny;
                    //1:右 2:左 3:下 4:上
                    //当前的[x,y]的值如果是符合1 2 3 4 的值,则不需要修改,也就是 0
                    int new_dist = dist[cur_pos] + (grid[x][y] == i + 1 ? 0 : 1);
                    if (nx < 0 || nx >= m || ny < 0 || ny >= n) {
                        continue;
                    }
                    if (new_dist < dist[new_pos]) {
                        dist[new_pos] = new_dist;
                        //01广度优先搜索,将0的那个点加入到队首,1的那个点加入到对尾
                        if (grid[x][y] == i + 1) {
                            deque.offerFirst(new_pos);
                        } else {
                            deque.offerLast(new_pos);
                        }
                    }
                }
            }
            return dist[m * n - 1];
        }

方法2:Dijkstra

        class Point {
            int dist;
            int r;
            int c;

            Point() {
            }

            Point(int dist, int r, int c) {
                this.dist = dist;
                this.r = r;
                this.c = c;
            }
        }

        int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

        public int minCost(int[][] grid) {
            int m = grid.length;
            int n = grid[0].length;
            boolean[][] vis = new boolean[m][n];
            int[][] dist = new int[m][n];
            for (int r = 0; r < m; r++) {
                Arrays.fill(dist[r], Integer.MAX_VALUE);
            }
            //按距离排序
            PriorityQueue<Point> pq = new PriorityQueue<>((o1, o2) -> o1.dist - o2.dist);
            pq.offer(new Point(0, 0, 0));
            dist[0][0] = 0;
            while (!pq.isEmpty()) {
                Point cur = pq.poll();
                int d = cur.dist, r = cur.r, c = cur.c;
                if (vis[r][c]) {
                    continue;
                }
                vis[r][c] = true;
                for (int k = 0; k < 4; k++) {
                    int nr = r + dirs[k][0], nc = c + dirs[k][1];
                    int cost = (k + 1 == grid[r][c] ? 0 : 1);
                    if (0 <= nr && nr < m && 0 <= nc && nc < n && dist[r][c] + cost < dist[nr][nc]) {
                        dist[nr][nc] = dist[r][c] + cost;
                        pq.offer(new Point(dist[nr][nc], nr, nc));
                    }
                }
            }
            return dist[m - 1][n - 1];
        }

方法3:Dijkstra

   
        int m, n;
        int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};


        public int minCost(int[][] grid) {
            m = grid.length;
            n = grid[0].length;
            int[] dist = new int[m * n];
            Arrays.fill(dist, Integer.MAX_VALUE);
            boolean[] vis = new boolean[m * n];
            dist[0] = 0;
            PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> dist[a] - dist[b]);
            pq.offer(0);
            while (!pq.isEmpty()) {
                //当前点位置
                int cur_pos = pq.poll();
                //注释掉下面的部分可以通过
//                if (vis[cur_pos]) {
//                    continue;
//                }
                vis[cur_pos] = true;
                //当前点的位置转化成坐标
                int x = cur_pos / n, y = cur_pos % n;
                for (int i = 0; i < 4; i++) {
                    int nx = x + dirs[i][0], ny = y + dirs[i][1];
                    if (nx < 0 || nx >= m || ny < 0 || ny >= n) {
                        continue;
                    }
                    int new_pos = nx * n + ny;
                    int new_dist = dist[cur_pos] + (grid[x][y] == i + 1 ? 0 : 1);
                    if (new_dist < dist[new_pos]) {
                        dist[new_pos] = new_dist;
                        pq.offer(new_pos);
                    }
                }
            }
            return dist[m * n - 1];
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值