#include <vector>
#include <iostream>
#include <climits>
#include <queue>
using namespace std;
/*
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.
*/
/*
Analyze:
we start from "1" value and write down the steps to reach each node. If the node is reachable, we remember the reachable status as "-1". Thus, we need to maintain two tables. One is to remember the steps, the other to remember the reachable status.
*/
// canAchieve value mark the achievable nodes layerly. If the node is not achievable, it will always be '0'.
void BFS(vector< vector<int> >& grid, int i, int j, vector< vector<int> >& steps, vector< vector<int> >& canReach, int& canAchieve) {
int m = grid.size();
int n = grid[0].size();
struct posAndStep {
int x;
int y;
posAndStep(int k, int h) : x(k), y(h) {};
};
vector< vector<int> > tmpStep(m, vector<int>(n, 0));
vector< vector<int> > dir {
{0, 1},
{0, -1},
{1, 0},
{-1, 0}};
queue<posAndStep> nodes;
posAndStep start(i, j);
nodes.push(start);
while(!nodes.empty()) {
auto curr = nodes.front();
nodes.pop();
for(auto direction : dir) {
int next_x = curr.x + direction[0];
int next_y = curr.y + direction[1];
if(next_x < m && next_x >= 0 && next_y < n && next_y >= 0 && (canReach[next_x][next_y] == canAchieve)) {
--canReach[next_x][next_y];
posAndStep next_node(next_x, next_y);
tmpStep[next_x][next_y] = tmpStep[curr.x][curr.y] + 1;
steps[next_x][next_y] += tmpStep[next_x][next_y];
nodes.push(next_node);
}
}
}
--canAchieve;
}
int shortestDistance(vector< vector<int> >& grid) {
if(grid.size() == 0 || grid[0].size() == 0) return 0;
int m = grid.size();
int n = grid[0].size();
vector< vector<int> > steps(m, vector<int>(n, 0));
vector< vector<int> > canReach = grid;
// BFS to cacualte the step and remember the status.
int canAchieve = 0;
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
if(grid[i][j] == 1) {
BFS(grid, i, j, steps, canReach, canAchieve);
}
}
}
int minDistant = INT_MAX;
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
if(steps[i][j] && canReach[i][j]) {
minDistant = min(minDistant, steps[i][j]);
}
}
}
return minDistant;
}
int main(void) {
vector< vector<int> > matrix{
{1, 0, 2, 0, 1},
{0, 0, 0, 0, 0},
{0, 0, 1, 0, 0}};
cout << shortestDistance(matrix) << endl;
}相关推荐
JavaScript实现拖拽元素对齐到网格(每次移动固定距离)
最近在做一个拖拽元素的附加功能,就是对齐到网格,实际上就是确定好元素的初始位置,然后拖拽元素时,每次移动固定的距离。让元素都可以在网格内对齐
2022-01-14:离建筑物最近的距离。 你是个房地产开发商,想要选择一片空地 建一栋大楼。你想把这栋大楼够造在一个距离周边设施都比较方便的地方,通过调研,你希望从它出发能在 最短的距离和 内抵达周
2022-01-14:离建筑物最近的距离。 你是个房地产开发商,想要选择一片空地 建一栋大楼。你想把这栋大楼够造在一个距离周边设施都比较方便的地方,通过调研,你希望从它出发能在 最短的距离和 内抵达周边全部的建筑物。请你计算出这个最佳的选址到周边全部建筑物的 最短距离和。 注意: 你只能通过向上、下、左、右四个方向上移动。 给你一个由 0、1 和 2 组成的二维网格,其中: 0 代表你可以自由通过和选择建造的空地; 1 代表你无非通行的建筑物; 2 代表你无非通行的障碍物。 来自力扣317。 答案2022-
Rust练手:从零手撸RISC-V架构虚拟机(一)(附完整代码)
使用Rust实现一个简单的RISC-V架构的CPU雏形,支持add,sub,addi三个指令。
【LeetCode】第二次总结。
题目汇总建筑物的最短距离寻找峰值只出现一次的数字课程表矩阵中的最长递增路径被围绕的区域情侣牵手寻找重复子树 计划对LeetCode刷题,顺便总结一下,四个阶段: 找出典型中等、困难题目,走读题目根据自己还记得的算法尝试回忆一遍 对照答案,识别自己的算法弱项,巩固算法后练习一遍典型习题 尝试找出同类型题目,巩固练习一遍 找出算法短板,重复1/2/3 算法总结 题目 我的算法 典型算法 建筑物的最短距离 暴力算法,挨个点计算距离和,然后得出最小的距离和 寻找峰值 暴力破解,遍历一次
树莓派4B学习笔记一
树莓派4b烧录Ubuntu20.04&&配置Ubuntu桌面
Sigrity OptimizePI Best Capacitor Location Estimation模式如何评估电容最佳位置操作指导
Sigrity OptimizePI Best Capacitor Location Estimation模式用于前仿真分析,评估电容的最佳位置以获得最小回路电感,如何分析,以下图为例进行说明。IC devices,输入U*, A*, B*, Q*, P*,将所有这些字母开头的器件都识别为IC。Min设置为1(最少电容个数为1),max设置为28(最多电容个数为28)Capacitors 输入C*,将所有C开头的器件都识别为电容。Grid X设置为8,Grid Y也设置为8。
LeetCode 317. 离建筑物最近的距离(逆向BFS)*
文章目录1. 题目2. 解题2.1 正常思维BFS2.2 逆向思考BFS 1. 题目 你是个房地产开发商,想要选择一片空地 建一栋大楼。 你想把这栋大楼够造在一个距离周边设施都比较方便的地方,通过调研,你希望从它出发能在 最短的距离和 内抵达周边全部的建筑物。 请你计算出这个最佳的选址到周边全部建筑物的 最短距离和。 提示: 你只能通过向上、下、左、右四个方向上移动。 给你一个由 0、1 和 2 组成的二维网格,其中: 0 代表你可以自由通过和选择建造的空地 1 代表你无法通行的建筑物 2 代表你无法通行
疾风气象大模型4.0白皮书
《疾风气象大模型白皮书摘要》 疾风气象大模型(J-AFM)是国内首个基于3D建模的气象智能平台,突破传统二维气象分析的局限,采用体素化技术构建"x-y-z-t"四维统一模型,实现从分钟级短临到30天跨时效的连续预测。核心技术包括:1)多源数据三维同化,融合雷达、卫星、风廓线等异构数据;2)物理约束AI解码器,嵌入能量守恒、湍流封闭等气象原理;3)行业适配插件,服务风电、光伏、航空等领域,如山地风场尾流校正、云影短临预测。平台支持批流一体推理与边缘部署,提供概率化输出和误差溯源能力,显著降
leetcode317 离建筑物最近的距离
https://leetcode-cn.com/problems/shortest-distance-from-all-buildings/ 正向思维的反面就是反向思维。从建筑物去找空地,然后在每个空地上累加建筑物可到达的数量,然后根据建筑物的数量找到满足条件的空地,再判断其他条件。 class Solution { private int[] rowDir = {0,0,1,-1}; private int[] colDir = {1,-1,0,0}; public int sh
STM32入门教程:智能储物柜控制
在这个例子中,我们将使用一个4x4的储物柜,共有16个储物格。我们了解了硬件准备、储物柜电路设计、STM32的基本配置、储物柜控制代码的编写以及测试和调试等方面的知识。根据我们的电路设计,我们需要将一些引脚配置为输出,并将其他引脚配置为输入。在本教程中,我们将使用STM32微控制器来实现一个基本的智能储物柜控制系统。在主函数中,我们将不断循环读取传感器数据,并根据数据的变化来控制储物柜的开关和显示相关信息。这个函数将读取人体感应传感器、温湿度传感器和光照传感器的数据,并将数据存储到相应的变量中。
Cadence OrCAD Capture CIS元器件管理时ERROR(ORCIS-6454): Temporary part number tracking is turned on报错的解决方法
使用Part manager对工程的元器件进行批量管理时,出现如下图所示的ERROR(ORCIS-6454): Temporary part number tracking is turned on报错其解决方法如下。
[Leetcode] 317. Shortest Distance from All Buildings
这题算是https://blog.csdn.net/chaochen1407/article/details/82720327的延伸。相当于那一题加了两个限定条件:1. 1本身不能成为聚集点。2.多了2这个数字代表obstacle。 加了这两个限定条件,就没办法采取那道题目的最优解决法了。目前所知的最优解就是遍历bfs。就是以每个building为起始点,然后往外bfs,对每个能达到的点进...
手把手教你完成YOLOv8分类识别界面搭建,使用Qt6设计YOLOv8分类识别系统,前台系统+后台管理系统开发实战,全网最详细教程
本项目基于YOLOv8分类模型,结合PySide6框架开发了一套图形化分类识别系统,支持图片、视频和摄像头实时检测。系统包含以下功能: 分类识别模块:完成图像/视频目标分类,界面展示检测结果。 用户管理模块:支持登录注册、头像修改、密码重置等功能。 AI问答模块:集成DeepSeek大模型接口,实现流式输出对话。 后台管理系统(第二版新增):管理员可管理用户信息及个人中心。 系统采用MySQL数据库存储用户数据,并提供完整的UI界面与交互逻辑。代码已开源,适用于毕业设计或快速部署场景。
[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 distance. You are given a 2D grid of values 0, 1 or 2, where:Each 0 marks an empty land which you can pa
计算机控制技术及应用
《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。
怀旧版电脑GBC模拟器
再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。
[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 distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 o
jdk-8u144-linux-x64.tar.gz.
该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包
LeetCode317 Shortest Distance from All Buildings
...
Devexpress16.2.6破解工具
Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具
LeetCode 317. Shortest Distance from All Buildings(最短建筑距离)
原题网址:https://leetcode.com/problems/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
Leetcode 317. Shortest Distance from All Buildings (python+cpp)
Leetcode 317. Shortest Distance from All Buildings题目解法1:BFS解法2: 题目 解法1:BFS brutal force的从每一个空地出发,找与building的最短距离之和 class Solution: def shortestDistance(self, grid: List[List[int]]) -> int: def helper(i,j): visited = set()
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 distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or ...
【Leetcode】317. Shortest Distance from All Buildings
题目地址: https://leetcode.com/problems/shortest-distance-from-all-buildings/ 给定一个二维矩阵,只含0,1,20,1,20,1,2,000代表空地,111代表建筑,222代表障碍物。要求在一个空地建个房屋,使得房屋到所有建筑物的路径长度之和达到最小。路径的每一步只能向上下左右走,并且只能走到空地上。如果存在房屋的建造方案,则返回最短路径长度总和;否则返回−1-1−1。 思路是BFS。可以从每个建筑物出发,遍历一下其所能到达的空地,并累加该
leetcode 317 shortest distance from all buildings
1 0 2 0 1 0 0 0 0 0 0 0 1 0 0 第一个building, 把涂色把0变成-1, 同一层最终会涂成相同颜色-1 1 -1 2 -1 1 -1 -1 -1 -1 -1 -1 -1 1 -1 ...
Leetcode 317. Shortest Distance from All Buildings(多次BFS)
Leetcode 317. Shortest Distance from All Buildings
[Swift]LeetCode317. 建筑物的最短距离 $ Shortest Distance from All Buildings
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...
[LeetCode] 317. Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on anemptyland 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 values0,1or2, wher...
2万+



被折叠的 条评论
为什么被折叠?



