描述


给定一个N x M的01矩阵,其中1表示陆地,0表示水域。对于每一个位置,求出它距离最近的水域的距离是多少。

矩阵中每个位置与它上下左右相邻的格子距离为1。

输入


第一行包含两个整数,N和M。

以下N行每行M个0或者1,代表地图。

数据保证至少有1块水域。

对于30%的数据,1 <= N, M <= 100

对于100%的数据,1 <= N, M <= 800

输出


输出N行,每行M个空格分隔的整数。每个整数表示该位置距离最近的水域的距离。

样例输入

4 4
0110
1111
1111
0110

样例输出

0 1 1 0
1 2 2 1
1 2 2 1
0 1 1 0

题解


以水域为起点,使用bfs搜索,一旦一个水域搜到一个陆地,其它水域就不必搜到这个陆地,因为这已经是最短路了

import java.util.*;
import java.io.*;
public class Main {
static final int N = 810;
static final int M = (int) 7e5 + 10;
static final int inf = 0x3f3f3f3f;
int[][] ans = new int[N][N];
char[][] a = new char[N][N];
int[][] book = new int[N][N];
int n, m;
class node {
public int x, y, step;
node() {
};
node(int tx, int ty, int s) {
x = tx;
y = ty;
step = s;
};
} int nex[][] = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } };
Queue<node> q = new LinkedList<>(); void bfs() {
node u = new node(), v = new node();
while (!q.isEmpty()) {
u = q.poll();
if (a[u.x][u.y] == '1')
ans[u.x][u.y] = Math.min(ans[u.x][u.y], u.step);
for (int i = 0; i < 4; i++) {
v.x = u.x + nex[i][0];
v.y = u.y + nex[i][1];
if (v.x >= n || v.y >= m || v.x < 0 || v.y < 0)
continue;
if (book[v.x][v.y] == 1)
continue;
book[v.x][v.y] = 1;
v.step = u.step + 1;
q.offer(new node(v.x, v.y, v.step));
}
}
} void solve() {
Scanner sc = new Scanner(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
n = sc.nextInt();
m = sc.nextInt();
for (int i = 0; i < n; i++)
Arrays.fill(ans[i], inf);
for (int i = 0; i < n; i++) {
a[i] = sc.next().toCharArray();
for (int j = 0; j < m; j++) {
if (a[i][j] == '0') {
q.offer(new node(i, j, 0));
ans[i][j] = 0;
}
}
}
bfs();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j > 0)
out.print(" ");
out.print(ans[i][j]);
}
out.println();
}
out.flush();
}
public static void main(String[] args) {
Main a = new Main();
a.solve();
}
}

【HIHOCODER 1478】 水陆距离(BFS)的更多相关文章

  1. HihoCoder - 1478 水陆距离

    水陆距离 描述 给定一个N x M的01矩阵,其中1表示陆地,0表示水域.对于每一个位置,求出它距离最近的水域的距离是多少. 矩阵中每个位置与它上下左右相邻的格子距离为1. 输入 第一行包含两个整数, ...

  2. hihocoder1478 水陆距离

    地址:http://hihocoder.com/problemset/problem/1478 题目: 水陆距离 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个 ...

  3. hihocoder offer收割编程练习赛9 B 水陆距离

    思路: 宽搜,多个起点. 实现: #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  4. 【[Offer收割]编程练习赛9 B】水陆距离

    [题目链接]:http://hihocoder.com/problemset/problem/1478 [题意] [题解] 一开始把所有的水域的位置都加入到队列中去; 然后跑一个bfs. 第一次到达的 ...

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

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

  6. H - 遥远的糖果 HihoCoder - 1478

    给定一个N x M的01矩阵,其中1表示人,0表示糖.对于每一个位置,求出每个位置离糖的最短距离是多少. 矩阵中每个位置与它上下左右相邻的格子距离为1. Input 第一行包含两个整数,N和M. 以下 ...

  7. Codeforces Round #361 (Div. 2) B bfs处理

    B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  8. [Offer收割]编程练习赛9,10

    题目1 : 闰秒 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 计算机系统中使用的UTC时间基于原子钟,这种计时方式同“地球自转一周是24小时”的计时方式有微小的偏差. ...

  9. hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. AForge.NET .NET2.0程序集无法在.net 4.0 中运行的解决方案

    如有雷同,不胜荣欣,若转载,请注明 最近在项目上一直使用.net4.0 framework,突然发现一个AForge.net中使用ffmepeg下的一个dll时,提示只能在2.0下运行,在众多MSDN ...

  2. Distance in Tree CodeForces - 161D

    Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...

  3. springmvc、springboot静态资源访问配置

    如何访问项目中的静态资源? 一.springmvc springmvc中访问静态资源,如果DispatcherServlet拦截的为"",那么静态资源的访问也会交给Dispatch ...

  4. hihocoder offer收割编程练习赛8 B 拆字游戏

    思路: 模拟,dfs. 注意题目中的trick,输出一块的时候不要把其他块也输出了. 实现: #include <cstring> #include <iostream> #i ...

  5. CF782A Andryusha and Socks

    题意: Andryusha is an orderly boy and likes to keep things in their place. Today he faced a problem to ...

  6. Dom 获取、Dom动态创建节点

    一.Dom获取 1.全称:Document     Object     Model 文档对象模型 2.我们常用的节点类型 元素(标签)节点.文本节点.属性节点(也就是标签里的属性). 3.docum ...

  7. 数据库SQL server 删除一张表中的重复记录

    --建立一张表 create table cat( catId int, catName varchar(40) ) --将下边的插入语句,多执行几次. insert into catvalues(1 ...

  8. gp服务输出的结果文件输出到绝对路径

    gp服务跟本地用arcmap执行gp有个不同,就是输出的文件一般只能输出到arcgis server默认的output目录里面(arcgis server有此限制,无论怎么配还是写到output目录里 ...

  9. Android Studio V4 V7 包冲突的问题

    最近被包冲突的问题搞奔溃了,特别是V4,V7 V4和V7包冲突的解决方式就是!版本要一致!! 比如我的一个项目中应用本来是这样引用包的 compile 'com.android.support:sup ...

  10. linux下自定义pid实现任意数据采集

    当你需要采集特殊的数据,而不满足于现有的你所知的数据模版时,自定义oid将是你必须而且非常好的解决方式. oid是snmp服务器为每条系统信息提供的唯一标识符,如果不能很好理解snmp服务的话,可以将 ...