1. 程式人生 > 程式設計 >.Net Core服務治理Consul搭建叢集

.Net Core服務治理Consul搭建叢集

給你一個m * n的網格,其中每個單元格不是0(空)就是1(障礙物)。每一步,您都可以在空白單元格中上、下、左、右移動。

如果您 最多 可以消除 k 個障礙物,請找出從左上角 (0, 0) 到右下角 (m-1, n-1) 的最短路徑,並返回通過該路徑所需的步數。如果找不到這樣的路徑,則返回 -1。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/shortest-path-in-a-grid-with-obstacles-elimination
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

深度優先搜尋 + 記憶化(錯誤)

dp陣列儲存的不一定是最優值

import java.util.Arrays;

class Solution {

    private static final int[][] directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

    private int[][][] dp;

    private int[][] grid;

    private boolean[][] visited;

    private int solve(int x, int y, int k) {
        if (dp[x][y][k] == -1) {
            if (grid[x][y] == 1 && k == 0) {
                dp[x][y][k] = Integer.MAX_VALUE;
                return Integer.MAX_VALUE;
            }

            if (x == grid.length - 1 && y == grid[0].length - 1) {
                dp[x][y][k] = 0;
                return 0;
            }

            if (grid[x][y] == 1) {
                k--;
            }

            int ans = Integer.MAX_VALUE;
            for (int i = 0; i < directions.length; ++i) {
                int nx = x + directions[i][0];
                int ny = y + directions[i][1];
                if (nx >= 0 && nx < grid.length && ny >= 0 && ny < grid[0].length && !visited[nx][ny]) {
                    visited[nx][ny] = true;
                    ans = Math.min(ans, solve(nx, ny, k));
                    visited[nx][ny] = false;
                }
            }
            dp[x][y][k] = ans == Integer.MAX_VALUE ? ans : ans + 1;
        }
        return dp[x][y][k];
    }

    public int shortestPath(int[][] grid, int k) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        this.grid = grid;
        this.visited = new boolean[grid.length][grid[0].length];
        this.dp = new int[grid.length][grid[0].length][k + 1];
        for (int i = 0; i < grid.length; ++i) {
            for (int j = 0; j < grid[0].length; ++j) {
                Arrays.fill(dp[i][j], -1);
            }
        }
        visited[0][0] = true;
        int ans = solve(0, 0, k);
        return ans == Integer.MAX_VALUE ? -1 : ans;
    }
}

廣度優先搜尋

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

class Solution {

    private static final int[][] directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};


    public int shortestPath(int[][] grid, int k) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        int m = grid.length, n = grid[0].length;
        if (m == 1 && n == 1) {
            return 0;
        }
        k = Math.min(m + n - 3, k);
        boolean[][][] visited = new boolean[m][n][k + 1];
        visited[0][0][k] = true;
        Queue<Info> queue = new LinkedList<>();
        queue.offer(new Info(0, 0, 0, k));
        while (!queue.isEmpty()) {
            Info node = queue.poll();
            if (node.x == m - 1 && node.y == n - 1) {
                return node.step;
            }
            for (int i = 0; i < directions.length; ++i) {
                int nx = node.x + directions[i][0];
                int ny = node.y + directions[i][1];
                if (nx >= 0 && nx < m && ny >= 0 && ny < n) {
                    if (grid[nx][ny] == 1) {
                        if (node.k > 0 && !visited[nx][ny][node.k - 1]) {
                            visited[nx][ny][node.k - 1] = true;
                            queue.offer(new Info(nx, ny, node.step + 1, node.k - 1));
                        }
                    } else {
                        if (!visited[nx][ny][node.k]) {
                            visited[nx][ny][node.k] = true;
                            queue.offer(new Info(nx, ny, node.step + 1, node.k));
                        }
                    }
                }
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        int[][] grid = {{0}};
        int k = 2;
        System.out.println(new Solution().shortestPath(grid, k));
    }
}

class Info {
    int x;
    int y;
    int step;
    int k;

    public Info(int x, int y, int step, int k) {
        this.x = x;
        this.y = y;
        this.step = step;
        this.k = k;
    }
}
心之所向,素履以往 生如逆旅,一葦以航