173. 矩阵距离(多源BFS)

173. 矩阵距离 - AcWing题库

给定一个 N 行 M 列的 0101 矩阵 A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为:

dist(A[i][j],A[k][l])=|i−k|+|j−l|

输出一个 N 行 M 列的整数矩阵 B,其中:

B[i][j]=min1≤x≤N,1≤y≤M,A[x][y]=1dist(A[i][j],A[x][y])

输入格式

第一行两个整数 N,M

接下来一个 N 行 M 列的 0101 矩阵,数字之间没有空格。

输出格式

一个 N 行 M 列的矩阵 B,相邻两个整数之间用一个空格隔开。

数据范围

1≤N,M≤1000

输入样例:
复制代码
3 4
0001
0011
0110
输出样例:
复制代码
3 2 1 0
2 1 0 0
1 0 0 1

解析 :

我们可以将所有得 1 作为起点,这样用bfs遍历即可得到任何一个点到1 得最短距离。

我们首先需将所有得1 先入栈

cpp 复制代码
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e3 + 5;
int n, m;
char g[N][N];
int v[N][N],d[N][N];

void bfs() {
	queue<PII>q;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (g[i][j] == '1') {
				q.push({ i,j });
				v[i][j] = 1;
			}
		}
	}

	int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
	while (!q.empty()) {
		PII t = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int a = t.first + dx[i], b = t.second + dy[i];
			if (a <= 0 || a > n || b <= 0 || b > m)continue;
			if (v[a][b])continue;
			q.push({ a,b });
			v[a][b] = 1;
			d[a][b] = d[t.first][t.second] + 1;
		}
	}
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		scanf("%s", g[i] + 1);
	}
	bfs();
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			printf("%d ", d[i][j]);
		}
		printf("\n");
	}
	return 0;
}
相关推荐
不知名的忻几秒前
Morris遍历(力扣第99题)
java·算法·leetcode·morris遍历
状元岐14 分钟前
C#反射从入门到精通
java·javascript·算法
_深海凉_1 小时前
LeetCode热题100-除了自身以外数组的乘积
数据结构·算法·leetcode
Kk.08022 小时前
项目《基于Linux下的mybash命令解释器》(一)
前端·javascript·算法
SteveSenna2 小时前
Trossen Arm MuJoCo自定义1:改变目标物体
人工智能·学习·算法·机器人
yong99902 小时前
IHAOAVOA:天鹰优化算法与非洲秃鹫优化算法的混合算法(Matlab实现)
开发语言·算法·matlab
米粒13 小时前
力扣算法刷题 Day 42(股票问题总结)
算法·leetcode·职场和发展
浅念-5 小时前
从LeetCode入门位运算:常见技巧与实战题目全解析
数据结构·数据库·c++·笔记·算法·leetcode·牛客
CoovallyAIHub5 小时前
无人机拍叶片→AI找缺陷:CEA-DETR改进RT-DETR做风电叶片表面缺陷检测,mAP50达89.4%
算法·架构·github
CoovallyAIHub5 小时前
混合训练反而更差?VLM Agent在训练前协调跨数据集标注,文档布局检测F-score从0.860提升至0.883
算法·架构·github