给定一个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. nodejs和npm

    Node.js安装及环境配置之Windows篇:https://www.cnblogs.com/liuqiyun/p/8133904.html 淘宝NPM镜像:https://npm.taobao.o ...

  2. O038、Shelve Instance 操作详解

    参考https://www.cnblogs.com/CloudMan6/p/5524751.html   Instance 被 Suspend 后虽然处于 shutdown 状态,但 Hypervis ...

  3. git 的用法和命令

    学无止境,精益求精! 十年河东,十年河西,莫欺少年穷! 学历代表你的过去,能力代表你的现在,学习代表你的将来! 很久没写博客了,都是工作太忙闹的,索性今儿转发一篇!省的博客园太冷清了... Git图形 ...

  4. Eclipse 设置新建文件默认编码为 utf-8 的方法

    选择编辑器顶部 Windows->Preferences->搜索jsp->选择utf-8编码->保存.

  5. vue入门:(v-for指令与列表渲染)

    v-for渲染列表 维护状态 数组变异方法与替换数组 $set.$remove 对象属性实现列表渲染 一.v-for渲染列表 语法:v-for="item in items" 先来 ...

  6. CSS3总结三:文字(text)/字体、文本、文本装饰、多列

    Text-Decoration text-shadow text-decoration Font font font-face Text 常用Text属性 Multi-column Multi-col ...

  7. pycharm问题合集

    一  打开pycharm出现 点击右上角的配置之后 配置正确的python路径 又出现 解决办法 删除所有的解释器,据说是重名导致的. 然后在配置一次 二  ModuleNotFoundError: ...

  8. ActiveMQ基础简介

    1. 什么是ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现 ...

  9. odoo 字段组件

    每个字段类型都会使用相应的默认组件在表单中显示.但还有一些替代组件可以使用.对于文本字段,有如下组件: email用于让 email 文本成为可操作的”mail-to”地址 url用于将文本格式化为可 ...

  10. python自动生成excel(xlwt库)

    下面代码使用web.py框架,其他框架都大同小异. # coding: utf- import web import json import datetime import xlwt import S ...