当前位置:网站首页>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;
}
};边栏推荐
- import torch_geometric 第一个图网络例子
- Further understanding of the network
- ITK multiresolution image itk:: recursivemultiresolutionpyramidimagefilter
- Improve pipeline efficiency: you need to know how to identify the main obstacles in ci/cd pipeline
- torch_geometric message passing network
- 【云原生 | Kubernetes篇】Ingress案例实战
- 达梦数据库DM8 windows环境安装
- leetcode 47. Permutations II full permutations II (medium)
- Unittest framework
- 用PyTorch进行语义分割
猜你喜欢

import torch_ Data view of geometric

微信web开发者工具使用教程,web开发问题

Five ways to quickly download large files from Google cloud disk

Successful job hopping Ali, advanced learning

The goods are full. You must take this knowledge

安装MySQL时出错,照着下面这个链接,做到cmd就不行了
![[EDA] chip layout design: VLSI layout design using electric](/img/a1/da0739070c940b36bc7a55d8eb2fe5.jpg)
[EDA] chip layout design: VLSI layout design using electric

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

入门深度学习与机器学习的经验和学习路径

Eight misunderstandings are broken one by one (2): poor performance? Fewer applications? You worry a lot about the cloud!
随机推荐
Design virtual network to realize communication between virtual machine instance and external network
5V升压到12.6V的锂电池充电IC芯片方案FS4062B
[EDA] chip layout design: VLSI layout design using electric
C#DBHelper_FactoryDB_GetConn
itk itk::BSplineDeformableTransform
R语言ggplot2可视化:使用ggrepel包在线图(line plot)的尾端那个数据点添加数值标签(number label)
【云原生 | Kubernetes篇】Ingress案例实战
A brief introduction to Verilog mode
Introduction to application design scheme of intelligent garbage can voice chip, wt588f02b-8s
ITK multiresolution image itk:: recursivemultiresolutionpyramidimagefilter
位图、布隆过滤器和哈希切分
【云原生 | Kubernetes篇】深入了解Deployment(八)
IC chip scheme fs4062b for lithium battery charging with 5V boost to 12.6V
Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem
中科物栖CEO张磊:“芯片+OS”范式在万物互联时代的机遇与挑战|量子位·视点分享回顾...
leetcode 47. Permutations II full permutations II (medium)
import torch_geometric 的Data 查看
Chaotic engineering practice of distributed kV storage in station B
It is enough to read this article. Web Chinese development
单向环形链表实现约瑟夫环