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,其 ...
随机推荐
- 【ExtJs】ext前台中的日期控件传输时间到后台的转换保存过程
//前台日期选择框 {fieldLabel:, padding: ',afterLabelTextTpl: required,allowBlank: false,format: 'Y-m-d H:i: ...
- OLE使用
ABAP操作EXCEL有多重方法,今天记录一下OLE,具体步骤如下: 1. 首先要上载EXCEL模板 事物代码:SMW0,具体步骤参考 本博客 http://www.cnblogs.com/caizj ...
- 解决:Nginx访问静态页面出现中文乱码
需要修改nginx的server的配置内容,增加一行:charset utf-8; 详情如下: upstream you.domainName.com { server 127.0.0.1:8080; ...
- ASP.NET实现验证码图片
新建一个checkcode.aspx文件,页面中不用写任何东西,在代码中,Page_Load中写入如下代码: string chkCode = string.Empty; int ix, ...
- 开源you-get项目爬虫,以及基于python+selenium的自动测试利器
写在前面 爬虫和自动测试,对于python来说是最合适不过也是最擅长的. 开源的项目也很多,例如you-get项目https://github.com/soimort/you-get.盗链和爬虫神器. ...
- ELK对nginx日志进行流量监控
ELK对nginx日志进行流量监控 一.前言 线上有一套ELK单机版,版本为5.2.1.现在想把nginx访问日志接入到elk里,进行各个域名使用流量带宽的统计分析.要把nginx日志传输到elk上, ...
- 两步创建vue全局组件
import Login from './Login' export default { install: function(Vue){ Vue.component('Login', Login); ...
- K-MEANS算法及sklearn实现
K-MEANS算法 聚类概念: 1.无监督问题:我们手里没有标签 2.聚类:相似的东西分到一组 3.难点:如何评估,如何调参 4.要得到簇的个数,需要指定K值 5.质心:均值,即向量各维取平均即可 6 ...
- 关于GRPC的讲解
gRPC服务发现&负载均衡 https://segmentfault.com/a/1190000008672912?utm_source=tag-newest GRPC编程指南 gRPC 介绍 ...
- angular8 集成swiper, 并将swiper封装成公共组件
安装Swiper npm install swiper --save 或者 yarn add swiper --save 在angular.json文件添加swiper.js和swiper.css ...