当前位置:网站首页>Edit distance (multi-source BFS)
Edit distance (multi-source BFS)
2022-07-06 12:56:00 【Classmate Chen_】
Title Description
Given a N That's ok M Column 01 matrix A,A[i][j] And A[k][l] The Manhattan distance between is defined as : dist(A[i][j],A[k][l])=|i−k|+|j−l| Output one N That's ok M The integer matrix of columns B, among : B[i][j]=min1≤x≤N,1≤y≤M,A[x][y]=1dist(A[i][j],A[x][y]) Input format
The first line has two integers N,M.
Next one N That's ok M Column 01 matrix , There is no space between the numbers .
Output format
One N That's ok M Columns of the matrix B, Two adjacent integers are separated by a space .
Data range
1≤N,M≤1000
sample input :
3 4
0001
0011
0110
sample output :
3 2 1 0
2 1 0 0
1 0 0 1
This topic is a multi-source BFS, Find all points to points as 1 And output the distance matrix .
We can divide everything into 1 Point of as the starting point , Then create a virtual source , The distance from the virtual source point to the starting point is 0.
If we do this, will the distance of the searched points be updated again when we search the points ?
The answer is no . Because single source BFS It has two phases
Because we search layer by layer , The point of each search must be bigger than yourself 1 The point of the layer , According to the two-phase property, we can deduce that it is monotonous .
Since it is monotonous , Then the points in front of us must be smaller than those added later , Therefore, the previously searched points will not be updated again .
In this way, we can turn the problem into a single source BFS, This question is doing single source BFS There is no need to create a real virtual source , Because the virtual source point to the starting point ( The value is 1 The point of ) The distance to 0, So when we do it, we can directly add all the starting points to the queue .
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
typedef pair<int,int> PII;
const int N=1005;
char g[N][N];
int d[N][N];
queue<PII> q;
int n,m;
void bfs(){
memset(d,-1,sizeof d);
int dx[]={
0,1,0,-1},dy[]={
1,0,-1,0};
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(g[i][j]=='1'){
// Find source point
d[i][j]=0;
q.push({
i,j});
}
}
}
while(q.size()){
PII t=q.front();
q.pop();
for(int i=0;i<4;i++){
// Search for
int a=t.first+dx[i],b=t.second+dy[i];
if(a<0 || a>=n || b<0 || b>=m) continue; // Is it across the border?
if(d[a][b]!=-1) continue; // Have you walked
q.push({
a,b});
d[a][b]=d[t.first][t.second]+1;
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>g[i][j];
}
}
bfs();
// Output distance matrix
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%d ",d[i][j]);
}
puts("");
}
return 0;
}
边栏推荐
- 染色法判定二分图
- MySQL performance tuning - dirty page refresh
- [算法] 剑指offer2 golang 面试题2:二进制加法
- [算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
- 1041 be unique (20 points (s)) (hash: find the first number that occurs once)
- Derivation of logistic regression theory
- Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
- InnoDB dirty page refresh mechanism checkpoint in MySQL
- idea中导包方法
- [algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
猜你喜欢
Naive Bayesian theory derivation
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
Database course design: college educational administration management system (including code)
FairyGUI人物状态弹窗
【无标题】
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
随机推荐
Compile GDAL source code with nmake (win10, vs2022)
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
idea中好用的快捷键
InnoDB dirty page refresh mechanism checkpoint in MySQL
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K
What are the advantages of using SQL in Excel VBA
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
Fabrication of fairygui simple Backpack
[algorithm] sword finger offer2 golang interview question 2: binary addition
Fairygui character status Popup
Halcon knowledge: gray_ Tophat transform and bottom cap transform
rtklib单点定位spp使用抗差估计遇到的问题及解决
VLSM variable length subnet mask partition tips
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
堆排序【手写小根堆】
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
基本Dos命令
[GNSS data processing] Helmert variance component estimation analysis and code implementation
Pride-pppar source code analysis
Basic DOS commands