图论-BFS


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

/**
 * 给你一个 m * n 的网格,其中每个单元格不是 0(空)就是 1(障碍物)。每一步,您都可以在空白单元格中上、下、左、右移动。
 * 如果您 最多 可以消除 k 个障碍物,请找出从左上角 (0, 0) 到右下角 (m-1, n-1) 的最短路径,并返回通过该路径所需的步数。如果找不到这样的路径,则返回 -1。
 *
 * 示例 1:
 * 输入:
 * grid =
 * [[0,0,0],
 *  [1,1,0],
 *  [0,0,0],
 *  [0,1,1],
 *  [0,0,0]],
 * k = 1
 * 输出:6
 * 解释:
 * 不消除任何障碍的最短路径是 10。
 * 消除位置 (3,2) 处的障碍后,最短路径是 6 。该路径是 (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).
 *  
 *
 * 示例 2:
 * 输入:
 * grid =
 * [[0,1,1],
 *  [1,1,1],
 *  [1,0,0]],
 * k = 1
 * 输出:-1
 * 解释:
 * 我们至少需要消除两个障碍才能找到这样的路径。
 *  
 *
 * 提示:
 * grid.length == m
 * grid[0].length == n
 * 1 <= m, n <= 40
 * 1 <= k <= m*n
 * grid[i][j] == 0 or 1
 * grid[0][0] == grid[m-1][n-1] == 0
 *
 */

public class BreadthFirstSearch {
    public static void main(String[] args) {
        int[][] grid = new int[][]{{0, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 1, 1}, {0, 0, 0}};
        Bfs tool = new Bfs();
        System.out.println(tool.shortestPath(grid, 1));
    }
}

class Bfs {
    private int m;
    private int n;
    private int k;
    private int[][] visited;
    private int minStep = 0;
    private Queue<Point> queue = new LinkedList<>();
    private int[] dx = {1, -1, 0, 0};
    private int[] dy = {0, 0, 1, -1};

    public int shortestPath(int[][] grid, int k) {
        init(grid, k);

        if (!isInputParamsValid()) {
            return -1;
        }

        if (m == 1 && n ==1) {
            return 0;
        }

        visited[0][0] = k;
        Point start = new Point(0, 0, 0);
        queue.offer(start);

        while (!queue.isEmpty()) {
            minStep++;

            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Point cut = queue.poll();
                int x = cut.getX();
                int y = cut.getY();
                int cnt = cut.getCnt();

                for (int j = 0; j < 4; j++) {
                    int xn = x + dx[j];
                    int yn = y + dy[j];

                    if (isOutSide(xn, yn)) {
                        continue;
                    }

                    if (isEndPoint(xn, yn)) {
                        return minStep;
                    }

                    if (grid[xn][yn] == 1 && cnt >= k) {
                        continue;
                    }

                    int newCnt = grid[xn][yn] == 1 ? cnt + 1 : cnt;
                    if (visited[xn][yn] != -1 && visited[xn][yn] >= k - newCnt) {
                        continue;
                    } else {
                        visited[xn][yn] = k - newCnt;
                    }
                    queue.offer(new Point(xn, yn, newCnt));
                }
            }
        }

        return -1;
    }

    private boolean isEndPoint(int xn, int yn) {
        return xn == m - 1 && yn == n - 1;
    }


    private boolean isOutSide(int xn, int yn) {
        if (xn < 0 || xn >= m || yn < 0 || yn >= n) {
            return true;
        }
        return false;
    }

    private void init(int[][] grid, int k) {
        this.m = grid.length;
        this.n = grid[0].length;
        this.k = k;

        visited = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                visited[i][j] = -1;
            }
        }
    }

    private boolean isInputParamsValid() {
        if (m < 1 || m > 40 || n < 1 || n > 40 || k < 1 || k > m * n) {
            return false;
        }
        return true;
    }
}

class Point {
    int x;
    int y;
    int cnt;

    public Point(int x, int y, int cnt) {
        this.x = x;
        this.y = y;
        this.cnt = cnt;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getCnt() {
        return cnt;
    }

    public void setCnt(int cnt) {
        this.cnt = cnt;
    }
}

输出结果:
6

你可能感兴趣的:(算法学习)