博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Shortest Distance from All Buildings
阅读量:6757 次
发布时间:2019-06-26

本文共 3060 字,大约阅读时间需要 10 分钟。

Shortest Distance from All Buildings

Given a 2D grid, each cell is either a wall 2, an house 1 or empty 0 (the number zero, one, two), find a place to build a post office so that the sum of the distance from the post office to all the houses is smallest.

Return the smallest sum of distance. Return -1 if it is not possible.

BFS

Time Complexity

O(kmn)
Space Complexity
O(mn)

思路

First, we need to clarify if it must has a place that can access to every house in matrix.

If it is not, we can improve our code by checking after each BFS, if there is still 0 in the grid and still hasn't visited, if there is, it means these 0 can't be the post office. Then we can turn these 0 into 2 as walls. So we don't need to count these 0 into cost any more.

Put all 1 into queue do BFS. We need to keep a global matrix called cost and add the distance from each 1 by doing BFS.

Each 1 need a new matrix for checking visited but we can keep only one matrix here to save space.

At last, checking cost matrix to find the min distance.

代码

public int shortestDistance(int[][] grid) {    // Write your code here    //corner case    if(grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0) return 0;        int rows = grid.length, cols = grid[0].length;    int[][] directions = new int[][]{
{-1,0},{1,0},{0,1},{0,-1}}; int[][] cost = new int[rows][cols]; for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(grid[i][j] == 1){ bfs(grid, cost, i, j, rows, cols, directions); } } } int minDis = Integer.MAX_VALUE; for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(grid[i][j] == 0 && cost[i][j] != 0){ minDis = Math.min(cost[i][j], minDis); } } } return minDis == Integer.MAX_VALUE ? -1 : minDis;} private void bfs(int[][] grid, int[][] cost, int i, int j, int rows, int cols, int[][] directions){ Queue
queue = new LinkedList
(); queue.offer(new int[]{i, j}); boolean[][] visited = new boolean[rows][cols]; visited[i][j] = true; int distance = 1; while(!queue.isEmpty()){ int size = queue.size(); for(int k = 0; k < size; k++){ int[] cur = queue.poll(); for(int[] dir : directions){ int x = cur[0] + dir[0]; int y = cur[1] + dir[1]; if(x >= 0 && x < rows && y >= 0 && y < cols && visited[x][y] == false && grid[x][y] == 0){ queue.offer(new int[]{x, y}); visited[x][y] = true; cost[x][y] += distance; } } } distance ++; } for(int r = 0; r < rows; r++){ for(int c = 0; c < cols; c++){ if(grid[r][c] == 0 && visited[r][c] == false){ grid[r][c] = 2; } } } }

转载地址:http://szzeo.baihongyu.com/

你可能感兴趣的文章
C#强化系列文章三:实验分析C#中三种计时器使用异同点
查看>>
Linux 进程间通信(一)
查看>>
通用对象池ObjectPool的一种简易设计和实现方案
查看>>
HTTP压缩仍让加密连接处于风险之中
查看>>
乐视阿里达成百亿元销售框架
查看>>
戴尔通过提升大数据分析能力巩固“全数据”战略 帮助企业在现代数据经济中蓬勃发展...
查看>>
⑤Windows Server 8 RemoteFX体验
查看>>
《企业云桌面实施》-小技巧-03-vSAN6.5中SAS和SSD的使用建议
查看>>
cocos2d-x学习笔记番外篇02:获取系统毫秒时间
查看>>
perl学习笔记(1)
查看>>
连接第三方 腾讯QQ家校.师生群向智慧教学一路狂奔
查看>>
简单三步,搞定“量产”Windows 2008
查看>>
excel查找替换转义问号
查看>>
初始化游戏状态数据
查看>>
delphi 显示窗体系统目录 源码
查看>>
PowerDesigner 业务处理模型( BPM ) 说明
查看>>
Redis内存存储结构分析
查看>>
OCP终于考完了
查看>>
Cocos2D:滚动滚屏黑边问题
查看>>
Android 4.1最终版SDK和ADT Plugin全线发布
查看>>