AcWing:173. 矩阵距离(bfs)
给定一个N行M列的01矩阵A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为:
输出一个N行M列的整数矩阵B,其中:
输入格式
第一行两个整数n,m。
接下来一个N行M列的01矩阵,数字之间没有空格。
输出格式
一个N行M列的矩阵B,相邻两个整数之间用一个空格隔开。
数据范围
1≤N,M≤10001≤N,M≤1000
输入样例:
3 4
0001
0011
0110
输出样例:
3 2 1 0
2 1 0 0
1 0 0 1
算法:bfs
题意:就是给你一个矩阵,然后矩阵里面有很多初始点,你需要求这些初始点到矩阵其他位置的最小距离。
#include <iostream>
#include <cstdio>
#include <queue> using namespace std; const int maxn = 1e3+; int n, m;
int Map[maxn][maxn];
int step[maxn][maxn];
int dir[][] = {, -, , , -, , , }; bool check(pair<int, int> next) {
if(next.first <= || next.first > n || next.second <= || next.second > m) {
return false;
}
if(step[next.first][next.second] != -) {
return false;
}
return true;
} void bfs() {
queue<pair<int, int> > q;
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
if(Map[i][j] == ) {
q.push(make_pair(i, j));
step[i][j] = ;
} else {
step[i][j] = -;
}
}
}
while(!q.empty()) {
pair<int, int> now = q.front();
q.pop();
for(int i = ; i < ; i++) {
pair<int, int> next;
next.first = now.first + dir[i][];
next.second = now.second + dir[i][];
if(check(next)) {
step[next.first][next.second] = step[now.first][now.second] + ;
q.push(next);
}
}
}
} int main() {
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
scanf("%1d", &Map[i][j]);
}
}
bfs();
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
printf("%d%c", step[i][j], " \n"[j == m]);
}
}
return ;
}
AcWing:173. 矩阵距离(bfs)的更多相关文章
- acwing 173. 矩阵距离(bfs)
给定一个N行M列的01矩阵A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为: dist(A[i][j],A[k][l])=|i−k|+|j−l|dist(A[i][j],A[k][l]) ...
- [BZOJ2252]矩阵距离(BFS)
题意 输入矩阵m行n列(m<=500,n<=500),只含0.1,输出离每个元素距离最近的1的距离,其中距离定义为D(aij,akl)=abs(i-k)+abs(j-l). 示例: 输入: ...
- AcWing P173 矩阵距离 题解
Analysis 就是一个裸的广搜,每次从是1的点开始找就好啦~~~ #include<iostream> #include<cstdio> #include<cstri ...
- BZOJ2252: [2010Beijing wc]矩阵距离
题解: 我脑子里都是翔??? bfs一下就行了 我居然还想什么kd tree!真是too naive,,, #include<cstdio> #include<cstdlib> ...
- BZOJ 2252: [2010Beijing wc]矩阵距离
题目 2252: [2010Beijing wc]矩阵距离 Time Limit: 10 Sec Memory Limit: 256 MB Description 假设我们有矩阵,其元素值非零即1 ...
- Bzoj 2252: [2010Beijing wc]矩阵距离 广搜
2252: [2010Beijing wc]矩阵距离 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 563 Solved: 274[Submit][ ...
- 「CH2501」 矩阵距离 解题报告
CH2501 矩阵距离 描述 给定一个N行M列的01矩阵 A,\(A[i][j]\) 与 \(A[k][l]\) 之间的曼哈顿距离定义为: \(dist(A[i][j],A[k][l])=|i-k|+ ...
- Acwing 蛇形矩阵
Acwing 蛇形矩阵 package javaqq; import java.util.Scanner; public class 蛇形 { public static void main(Stri ...
- 2501 矩阵距离 (bfs)
描述 给定一个N行M列的01矩阵 A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为: dist(A[i][j],A[k][l])=|i-k|+|j-l| 输出一个N行M列的整数矩阵B,其 ...
随机推荐
- CF 666C & 牛客 36D
给定字符串S, 求有多少长为$n$的字符串T, 使得S为T的子序列. 可以得到转移矩阵为 $\begin{equation}A=\begin{bmatrix}25 & 0 & 0 &a ...
- the specified service is marked as deletion,can not find the file specified
使用命令注册windows service sc create CCGSQueueService binpath= "D:\DKX4003\services\xxx.xx.xx\xxx.ex ...
- NetScaler循环抓包设置
NetScaler循环抓包设置 来源 https://raynorli.com/2018/07/01/netscaler-nstrace-cycling-capture/ 参考文档 How to Re ...
- 单元操作和仓储模式 repository+unitOfWork
仓储和工作单元模式是用来在数据访问层和业务逻辑层之间创建一个抽象层.应用这些模式,可以帮助用来隔离你的程序在数据存储变化. 在数据源层和业务层之间增加一个repository层进行协调,有如下作用:1 ...
- 异常-Throwable的几个常见方法
package cn.itcast_04; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...
- O033、Terminate Instance 操作详解
参考https://www.cnblogs.com/CloudMan6/p/5486066.html 本节通过日志详细分析 Nova Terminate 操作.Terminate 操作就是删除 i ...
- eclipse设置字体
- Html-自适应
自适应 使网页能适应不同终端设备的技术.原理是通过检测视口分辨率来判断是什么终端的,PC,手机还是平板. 做自适应的网页时,需要在代码中加入“祖传代码”,即通用代码. 这是在头部head引入的: &l ...
- Python中GUI库PyQt5的安装和配置
在使用Tkinter开发GUI程序时,发现相关文档比较少,开发起来太累.经过综合比较,决定使用PyQt这个库.下面是简单的安装步骤. 1.安装 PyQt5 : pip install PyQt5 -i ...
- go语言入门(2)数据类型
1,命名 Go语言中的函数名.变量名.常量名.类型名.语句标号和包名等所有的命名,都遵循一个简单的命名规则:一个名字必须以一个字母(Unicode字母)或下划线开头,后面可以跟任意数量的字母.数字或下 ...