给定一个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])=|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])B[i][j]=min1≤x≤N,1≤y≤M,A[x][y]=1⁡dist(A[i][j],A[x][y])

输入格式

第一行两个整数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)的更多相关文章

  1. 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]) ...

  2. [BZOJ2252]矩阵距离(BFS)

    题意 输入矩阵m行n列(m<=500,n<=500),只含0.1,输出离每个元素距离最近的1的距离,其中距离定义为D(aij,akl)=abs(i-k)+abs(j-l). 示例: 输入: ...

  3. AcWing P173 矩阵距离 题解

    Analysis 就是一个裸的广搜,每次从是1的点开始找就好啦~~~ #include<iostream> #include<cstdio> #include<cstri ...

  4. BZOJ2252: [2010Beijing wc]矩阵距离

    题解: 我脑子里都是翔??? bfs一下就行了 我居然还想什么kd tree!真是too naive,,, #include<cstdio> #include<cstdlib> ...

  5. BZOJ 2252: [2010Beijing wc]矩阵距离

    题目 2252: [2010Beijing wc]矩阵距离 Time Limit: 10 Sec  Memory Limit: 256 MB Description 假设我们有矩阵,其元素值非零即1 ...

  6. Bzoj 2252: [2010Beijing wc]矩阵距离 广搜

    2252: [2010Beijing wc]矩阵距离 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 563  Solved: 274[Submit][ ...

  7. 「CH2501」 矩阵距离 解题报告

    CH2501 矩阵距离 描述 给定一个N行M列的01矩阵 A,\(A[i][j]\) 与 \(A[k][l]\) 之间的曼哈顿距离定义为: \(dist(A[i][j],A[k][l])=|i-k|+ ...

  8. Acwing 蛇形矩阵

    Acwing 蛇形矩阵 package javaqq; import java.util.Scanner; public class 蛇形 { public static void main(Stri ...

  9. 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,其 ...

随机推荐

  1. jenkins 设置中文显示

    这里使用的方法是安装中文语言包,安装的插件名称是:Localization: Chinese (Simplified) 1.在插件管理,搜索 Localization: Chinese (Simpli ...

  2. kafka运维填坑

    转载自:https://www.jianshu.com/p/d2cbaae38014 前提: 只针对Kafka 0.9.0.1版本; 说是运维,其实偏重于问题解决; 大部分解决方案都是google而来 ...

  3. Oracle update 多字段更新

    一次性update多个字段 以student表为例: -- 创建学生表 create table student ( id number, name varchar2(40), age number, ...

  4. Redis之淘汰策略

    Redis 内存数据集大小上升到一定大小的时候,就会进行数据淘汰策略. Redis 提供了 6 种数据淘汰策略: 1. volatile-lru:从已设置过期时间的数据集中挑选最近最少使用的数据淘汰. ...

  5. MySQL增删改查语句

    创建数据库:CREATE DATABASE 数据库名; 创建数据表:CREATE TABLE table_name (column_name column_type); 插入数据:INSERT INT ...

  6. Java实现文本中的关键字高亮,匹配所有长度

    这个方法还不完整,后面想起来再看,直接放代码 public static String getHeightlightWord(String textWord, String key){ StringB ...

  7. 5、Sersync实时同步实战

    1.实时同步概述 1.什么是实时同步, 只要当前目录发生变化则会触发一个事件,事件触发后将变化的目录同步至远程服务器. 2.为什么要实时同步, 保证数据的连续性, 减少人力维护成本, 解决nfs单点故 ...

  8. 08-【jsp重点】

    jsp的四个作用域和9个内置对象 jsp内置对象[重点]:pageContext.request.session.application.response.out.page.exception.con ...

  9. Linux工具之top

    top命令详解: 第一行:10:01:23----当前系统时间   126days,14:29------系统已经运行了126天14小时29分钟(在这期间没有重启过)   2users------当前 ...

  10. Spinner simpleAdapte适配器 下拉列表

    public class MainActivity extends AppCompatActivity { private TextView text; private Spinner spinner ...