当前位置:网站首页>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;
}
边栏推荐
- Fabrication d'un sac à dos simple fairygui
- [算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
- FairyGUI增益BUFF数值改变的显示
- IText 7 generate PDF summary
- [leetcode622] design circular queue
- Wechat applet development experience
- 服务未正常关闭导致端口被占用
- [算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
- Combination of fairygui check box and progress bar
- Problems and solutions of robust estimation in rtklib single point location spp
猜你喜欢

Basic DOS commands

染色法判定二分图

idea问题记录
![[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积](/img/e0/cea31070d6365eb57013cdead4a175.png)
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
![[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和](/img/d5/4bda133498f71ae9fd7a64c6cba8f0.png)
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
![[算法] 剑指offer2 golang 面试题4:只出现一次的数字](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[算法] 剑指offer2 golang 面试题4:只出现一次的数字

There is no red exclamation mark after SVN update
![[algorithm] sword finger offer2 golang interview question 5: maximum product of word length](/img/e0/cea31070d6365eb57013cdead4a175.png)
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length

Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)

C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
随机推荐
FairyGUI摇杆
[算法] 剑指offer2 golang 面试题1:整数除法
Itext 7 生成PDF总结
Fabrication d'un sac à dos simple fairygui
MySQL performance tuning - dirty page refresh
FairyGUI增益BUFF數值改變的顯示
Guided package method in idea
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
Unity3d makes the registration login interface and realizes the scene jump
Implementation of Excel import and export functions
平衡二叉树详解 通俗易懂
Teach you to release a DeNO module hand in hand
《软件测试》习题答案:第一章
Office提示您的许可证不是正版弹框解决
[untitled]
Prove the time complexity of heap sorting
[Chongqing Guangdong education] Shandong University College Physics reference materials
雇佣收银员【差分约束】
In 2020, the average salary of IT industry exceeded 170000, ranking first
Compile GDAL source code with nmake (win10, vs2022)