当前位置:网站首页>Multi source BFS problem template (with questions)
Multi source BFS problem template (with questions)
2022-06-12 13:18:00 【Twilight_ years】
Title Description : There are multiple starting points , Find the shortest path from all points to different starting points .
practice : Add all starting points to the queue and use bfs To do it .
Simple proof :
You can add a virtual starting point before all the starting points , The path length from this virtual starting point to all starting points is 0.

Then find the shortest distance from all points to the starting point , Is to find the shortest distance from all points to this virtual starting point .
Since the weight from the virtual starting point to all edges is 0, So you can add all the starting points to the queue .
1162. As Far from Land as Possible
Given an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance. If no land or water exists in the grid, return -1.
The distance used in this problem is the Manhattan distance: the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|
The question : Please all 0 Medium to all 1 The maximum value of the shortest distance .

hold 1 Add to the queue as a starting point , And then use bfs To do it .
#define x first
#define y second
typedef pair<int,int> PII;
class Solution {
public:
int maxDistance(vector<vector<int>>& g) {
int n=g.size(),m=g[0].size(),INF=1e8;
vector<vector<int>>dist(n,vector<int>(m,INF));
queue<PII>q;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(g[i][j]){
dist[i][j]=0;
q.push({i,j});
}
}
}
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
while(q.size()){
auto t =q.front();
q.pop();
for(int i=0;i<4;i++){
int x=t.x+dx[i],y=t.y+dy[i];
if(x>=0&&y>=0&&x<n&&y<m&&dist[x][y]==INF){
dist[x][y]=dist[t.x][t.y]+1;
q.push({x,y});
}
}
}
int res=-1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!g[i][j]){
res=max(res,dist[i][j]);
}
}
}
if(res==INF)res=-1;
return res;
}
};边栏推荐
- MUI登录数据库完善与AJAX异步处理【MUI+Flask+MongoDB+HBuilderX】
- Pre research of image scanning tool
- How to balance multiple losses in deep learning?
- [embedded] serial communication and its case
- [cloud native | kubernetes] learn more about ingress
- Install MySQL database independently on Debian 10
- Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem
- 成功定级腾讯T3-2,万字解析
- Deploy opengauss database based on Huawei cloud Kunpeng elastic ECS [Gauss is not a mathematician this time]
- leetcode 47. Permutations II full permutations II (medium)
猜你喜欢

The goods are full. You must take this knowledge

单向环形链表实现约瑟夫环

构建嵌入式系统软件开发环境-建立交叉编译环境

成功定级腾讯T3-2,万字解析

What is the function tag? Article to understand its role and its best practices

创新实训(十)高级界面美化

torch_geometric mini batch 的那些事

Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题

【微信小程序开发】第1篇:开发工具安装及程序配置

It is enough to read this article. Web Chinese development
随机推荐
itk::SymmetricForcesDemonsRegistrationFilter
嵌入式系统概述2-嵌入式系统组成和应用
ITK Examples/RegistrationITKv4/DeformableRegistration
Online picture material
442 authors, 100 pages! It took Google 2 years to release the new benchmark big bench | open source
Eight misunderstandings are broken one by one (2): poor performance? Fewer applications? You worry a lot about the cloud!
Application of list and Dict
在 Debian 10 上独立安装MySQL数据库
Getting started with NVIDIA Jetson nano Developer Kit
微信web开发者工具使用教程,web开发问题
Mui login database improvement and Ajax asynchronous processing [mui+flask+mongodb+hbuilderx]
多源BFS问题 模板(附题)
Three dimensional coordinate point fitting sphere (MATLAB and C)
Semantic segmentation with pytorch
itk itk::BSplineDeformableTransform
嵌入式系统概述3-嵌入式系统的开发流程和学习基础、方法
JVM runtime parameters
Getting to know blob objects
【微信小程序开发】第1篇:开发工具安装及程序配置
torch_geometric message passing network