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 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.

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.

分析:

  这题如果不考虑obstacle的存在的话,与另一道leetcode题目一样,分成x轴和y轴,根据值为1的点的坐标,直接算出最小距离;然而多了一个obstacle,这题又更像是gates and walls这题了,不同的是,对于每个为0的点,各个建筑物到它的最近的距离都要计算出来并累加,而不是算最近距离的最小值。K为building个数,M、N分别为长和宽,时间复杂度为O(KMN),空间复杂度为O(MN)

代码:

//计算每个岛到坐标为(i, j)的building的最短距离
void dfs(int i, int j, int cur, vector<vector<int> > &dist, vector<vector<int> > &grids) {
if(cur > dist[i][j])
return;
dist[i][j] = cur++;
if(grids[i][j + ] == )
dfs(i, j + , cur, dist, grids);
if(grids[i][j - ] == )
dfs(i, j - , cur, dist, grids);
if(grids[i + ][j] == )
dfs(i + , j, cur, dist, grids);
if(grids[i - ][j] == )
dfs(i - , j, cur, dist, grids);
return;
}
//迭代计算总距离矩阵,并重置距离矩阵
void postProcess(vector<vector<int> > &dist, vector<vector<int> > &totaldist, vector<vector<int> > &grids) {
for(int i = ; i < grids.size(); i++)
for(int j = ; j < grids[].size(); j++) {
if(grids[i][j] == )
totaldist[i][j] += dist[i][j];
dist[i][j] = INT_MAX;
}
return;
}
//主要功能函数
int shortestDist(vector<vector<int> > &grids) {
//设立岗哨
grids.insert(grids.begin(), vector<int> (grids[].size(), ));
grids.push_back(vector<int> (grids[].size(), ));
for(auto &v : grids) {
v.insert(v.begin(), );
v.push_back();
}
//声明并初始化距离矩阵
vector<vector<int> > dist(grids);
for(auto &v : dist)
for(int &i : v)
i = INT_MAX;
//声明并初始化总距离矩阵
vector<vector<int> > totaldist(grids.size(), vector<int> (grids[].size(), ));
//对每个building进行扩展,计算其到周边岛屿的最小距离
for(int i = ; i < grids.size(); i++) {
for(int j = ; j < grids[].size(); j++) {
if(grids[i][j] == ) {
dfs(i, j, , dist, grids);
postProcess(dist, totaldist, grids);
}
}
}
//在总距离矩阵中找到最小距离
int sd = INT_MAX;
for(int i = ; i < grids.size(); i++)
for(int j = ; j < grids[].size(); j++)
if(grids[i][j] == )
sd = min(sd, totaldist[i][j]);
//去除岗哨,还原输入矩阵
grids.pop_back();
grids.erase(grids.begin());
for(auto &v : grids) {
v.pop_back();
v.erase(v.begin());
}
return sd;
}

[Locked] Shortest Distance from All Buildings的更多相关文章

  1. leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings

    542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...

  2. [LeetCode] 317. Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  3. Shortest Distance from All Buildings

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  4. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  5. LeetCode Shortest Distance from All Buildings

    原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...

  6. 317. Shortest Distance from All Buildings

    题目: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where th ...

  7. [Swift]LeetCode317. 建筑物的最短距离 $ Shortest Distance from All Buildings

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  8. [LeetCode] Shortest Distance from All Buildings Solution

    之前听朋友说LeetCode出了一道新题,但是一直在TLE,我就找时间做了一下.这题是一个比较典型的BFS的题目,自己匆忙写了一个答案,没有考虑优化的问题,应该是有更好的解法的. 原题如下: You ...

  9. LeetCode 317. Shortest Distance from All Buildings

    原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...

随机推荐

  1. HashMap深度解析(一)

    本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/16843543 HashMap可以说是Java中最常用的集合类框架之一,是 ...

  2. 用sed、awk、grep同时匹配多个条件(与模式、或模式)

    同时匹配ABC 和 123:sed -n '/ABC/{/123/p}' awk '/ABC/&&/123/{ print $0 }' grep -E '(ABC.*123|123.* ...

  3. Oracle分区表学习

    (1) 表空间及分区表的概念表空间: 是一个或多个数据文件的集合,所有的数据对象都存放在指定的表空间中,但主要存放的是表, 所以称作表空间.分区表: 当表中的数据量不断增大,查询数据的速度就会变慢,应 ...

  4. Windows 8 / 8.1 禁用驱动签名最详细图文教程

    Windows 8 鼠标右上角/右下角弹出边栏,选择“设置” 点击“更改电脑设置”: 选择“常规”,右侧拖到底,点击“高级启动”-“立即重启”: 稍后进入恢复页面,选择“疑难解答” 进入疑难解答后,选 ...

  5. JavaMail API 1.4.7邮件发送

    下载oracle javaMail API: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive- ...

  6. LA 6856 Circle of digits 解题报告

    题目链接 先用后缀数组给串排好序.dc3 O(n) 二分答案+贪心check 答案的长度len=(n+k-1)/k 如果起点为i长为len串大于当前枚举的答案,i的长度取len-1 从起点判断k个串的 ...

  7. Invoke()/BeginInvoke()区别

    查看MSDN如下: Control..::.Invoke          ---> 在拥有此控件的基础窗口句柄的线程上执行委托. Control..::.BeginInvoke  ---> ...

  8. 最优秀的5个Linux文本编辑器

    from: http://article.yeeyan.org/view/169956/174836 作为不久前举办的比赛的一部分内容,我从那些选出他们最喜欢的Linux文本编辑器的极客读者们那获得了 ...

  9. Ubuntu phpmyadmin 缺少mcrypt扩展解决方法

    之前在登陆phpmyadmin的时候,会出现警告说缺少mcrypt扩展的错误,一直没去解决这个问题,觉得没什么影响就算了. 今天谷歌了一下,原来是php5没有启用mcrypt模块.   sudo ph ...

  10. iOS工程适配64-bit经验分享

    终究还是来了.Apple下发了支持64位的最后通牒: As we announced in October, beginning February 1, 2015 new iOS apps submi ...