【HIHOCODER 1478】 水陆距离(BFS)
描述
给定一个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)的更多相关文章
- HihoCoder - 1478 水陆距离
水陆距离 描述 给定一个N x M的01矩阵,其中1表示陆地,0表示水域.对于每一个位置,求出它距离最近的水域的距离是多少. 矩阵中每个位置与它上下左右相邻的格子距离为1. 输入 第一行包含两个整数, ...
- hihocoder1478 水陆距离
地址:http://hihocoder.com/problemset/problem/1478 题目: 水陆距离 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个 ...
- hihocoder offer收割编程练习赛9 B 水陆距离
思路: 宽搜,多个起点. 实现: #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- 【[Offer收割]编程练习赛9 B】水陆距离
[题目链接]:http://hihocoder.com/problemset/problem/1478 [题意] [题解] 一开始把所有的水域的位置都加入到队列中去; 然后跑一个bfs. 第一次到达的 ...
- [BZOJ2252]矩阵距离(BFS)
题意 输入矩阵m行n列(m<=500,n<=500),只含0.1,输出离每个元素距离最近的1的距离,其中距离定义为D(aij,akl)=abs(i-k)+abs(j-l). 示例: 输入: ...
- H - 遥远的糖果 HihoCoder - 1478
给定一个N x M的01矩阵,其中1表示人,0表示糖.对于每一个位置,求出每个位置离糖的最短距离是多少. 矩阵中每个位置与它上下左右相邻的格子距离为1. Input 第一行包含两个整数,N和M. 以下 ...
- 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 ...
- [Offer收割]编程练习赛9,10
题目1 : 闰秒 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 计算机系统中使用的UTC时间基于原子钟,这种计时方式同“地球自转一周是24小时”的计时方式有微小的偏差. ...
- hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- 统计Apache或nginx日志里访问次数最多的前十个IP
1.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l 2.统计访问URL统计PV awk '{print $7}' access ...
- Codeforces Round #261 (Div. 2) C
Description Recently Pashmak has been employed in a transportation company. The company has kbuses a ...
- April Fools Contest 2017 E
Description Input The input consists of four lines, each line containing a single digit 0 or 1. Outp ...
- Reduce实现
Reduce实现 参考 第一版 Array.prototype.fakeReduce = function (fn, base) { // this 指向原数组 // 拷贝数据, 更改指针方向 var ...
- 508 Most Frequent Subtree Sum 出现频率最高的子树和
详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/ C++: /** * Definition for a ...
- java中的compareto方法以及LIst列表排序的详细介绍【转】
java中的compareto方法的详细介绍 javacompareTo java中的compareto方法,返回参与比较的前后两个字符串的asc码的差值,看下面一组代码 String a=&quo ...
- json和php数组 格式的互相转换
$json_arr = array('WebName'=>'PHP网站开发教程网','WebSite'=>'http://www.jb51.net'); $php_json = json ...
- 多线程wait和notify实现1212
package threadT; public class ThreadMain { public static void main(String args[]) { final Object obj ...
- logging模块进阶2
1.两种级别设置: 全局级别:生成logger对象后设置的级别 局部级别:生成handler对象设置的级别 我们都知道输出的级别不能低于设定的级别,那么全局级别和局部级别哪一个对输出产生影响? 经过多 ...
- JVM补充一
一.为什么废弃永久代(PermGen) 2.1 官方说明 参照JEP122:http://openjdk.java.net/jeps/122,原文截取: Motivation This is part ...