Tuesday, October 11, 2016

Shortest Distance from All Buildings

You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 01 or 2, where:
  • Each 0 marks an empty land which you can pass by freely.
  • Each 1 marks a building which you cannot pass through.
  • Each 2 marks an obstacle which you cannot pass through.
For example, given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2):
1 - 0 - 2 - 0 - 1
|   |   |   |   |
0 - 0 - 0 - 0 - 0
|   |   |   |   |
0 - 0 - 1 - 0 - 0
The point (1,2) is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal. So return 7.
Note:
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.

Using BFS. For every building, get distance from it to all empty spaces. Then sum up and find minimum. In order to avoid creating to many tracking matrices, every time we finishing updating distance for one building, decrement the empty space by 1, thus we know this space is visited and is valid for next building to traverse.


    public int shortestDistance(int[][] grid) {
        if (grid.length == 0 || grid[0].length == 0) {
            return -1;
        }
        int m = grid.length, n = grid[0].length;
        int[][] sum = new int[m][n];
        int[][] neighbors = {
            {-1, 0},
            {1, 0},
            {0, -1},
            {0, 1}
        };

        int val = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    findMin(grid, sum, neighbors, i, j, val);
                    val--;
                }
            }
        }
        int rst = Integer.MAX_VALUE;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] < 0) {
                    rst = Math.min(rst, sum[i][j]);
                }
            }
        }
        return rst;
    }

    private void findMin(int[][] grid, int[][]sum, int[][] neighbors, int i, int j, int val) {
        int m = grid.length, n = grid[0].length;
        int[][] dist = new int[grid.length][grid[0].length];
        Queue queue = new LinkedList<>();
        queue.add(i * n + j);
        while (!queue.isEmpty()) {
            int curr = queue.poll();
            int x = curr / n;
            int y = curr % n;
            for (int[] nei : neighbors) {
                int x1 = x + nei[0];
                int y1 = y + nei[1];
                if (x1 >= 0 && x1 < m && y1 >= 0 && y1 < n && grid[x1][y1] == val) {
                    dist[x1][y1] = dist[x][y] + 1;
                    grid[x1][y1]--;
                    sum[x1][y1] += dist[x1][y1];
                    queue.add(x1 * n + y1);
                }
            }
        }
    }


1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete