美文网首页
20. Shortest Distance from All B

20. Shortest Distance from All B

作者: 邓博文_7c0a | 来源:发表于2018-01-26 08:14 被阅读0次

    Link to the problem

    Description

    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 0, 1 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.

    Note:
    There will be at least one building. If it is not possible to build such house according to the above rules, return -1.

    Example

    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.

    Idea

    From each building, do a level order traversal of the graph. Increment each cell with the distance to that building, and increment the count. Only consider empty slots that are reachable by all buildings.

    Solution

    class Solution {
    public:
        int shortestDistance(vector<vector<int> > &grid) {
            int n_row = grid.size();
            if (!n_row) return -1;
            int n_col = grid[0].size();
            if (!n_col) return -1;
            vector<vector<int> > count(n_row, vector<int>(n_col, 0));
            vector<vector<int> > dist(n_row, vector<int>(n_col, 0));
            vector<pair<int, int> > didj = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
            int n_house = 0;
            for (int i = 0; i < n_row; i++) {
                for (int j = 0; j < n_col; j++) {
                    if (grid[i][j] != 1) continue;
                    ++n_house;
                    vector<pair<int, int> > level = {{i, j}};
                    vector<vector<bool> > visited(n_row, vector<bool>(n_col, false));
                    visited[i][j] = true;
                    int n_level = 0;
                    while (!level.empty()) {
                        ++n_level;
                        vector<pair<int, int> > new_level;
                        for (auto it = level.begin(); it != level.end(); ++it) {
                            int row = it->first, col = it->second;
                            for (auto diff = didj.begin(); diff != didj.end(); ++diff) {
                                int new_row = row + diff->first, new_col = col + diff->second;
                                if (0 <= new_row && new_row < n_row &&
                                   0 <= new_col && new_col < n_col &&
                                   !visited[new_row][new_col] && grid[new_row][new_col] == 0) {
                                    visited[new_row][new_col] = true;
                                    dist[new_row][new_col] += n_level;
                                    ++count[new_row][new_col];
                                    new_level.push_back(pair<int, int> {new_row, new_col});
                                }
                            }
                        }
                        level = new_level;
                    }
                }
            }
            int rtn = INT_MAX;
            for (int i = 0; i < n_row; ++i) {
                for (int j = 0; j < n_col; ++j) {
                    if (grid[i][j] == 0 && count[i][j] == n_house) {
                        rtn = min(rtn, dist[i][j]);
                    }
                }
            }
            return (rtn == INT_MAX) ? -1 : rtn;
        }
    };
    

    72 / 72 test cases passed.
    Runtime: 82 ms

    相关文章

      网友评论

          本文标题:20. Shortest Distance from All B

          本文链接:https://www.haomeiwen.com/subject/ykhiaxtx.html