1162. 地图分析

你现在手里有一份大小为 N x N 的『地图』(网格) grid,上面的每个『区域』(单元格)都用 0 和 1 标记好了。其中 0 代表海洋,1 代表陆地,你知道距离陆地区域最远的海洋区域是是哪一个吗?请返回该海洋区域到离它最近的陆地区域的距离。(PS:你看看这个翻译)

我们这里说的距离是『曼哈顿距离』( Manhattan Distance):(x0, y0) 和 (x1, y1) 这两个区域之间的距离是 |x0 - x1| + |y0 - y1| 。

如果我们的地图上只有陆地或者海洋,请返回 -1。

示例 1:

输入:[[1,0,1],[0,0,0],[1,0,1]]

输出:2

解释:

海洋区域 (1, 1) 和所有陆地区域之间的距离都达到最大,最大距离为 2。

示例 2:

输入:[[1,0,0],[0,0,0],[0,0,0]]

输出:4

解释:

海洋区域 (2, 2) 和所有陆地区域之间的距离都达到最大,最大距离为 4。

提示:

1 <= grid.length == grid[0].length <= 100

grid[i][j] 不是 0 就是 1

class Solution {
public int maxDistance(int[][] grid) {
int m=grid.length;
int n=grid[0].length;
boolean f=false;
//从两个方法查询,并且是正反查询 for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==1){
f=true;
continue;
} if(grid[i][j]==0){
grid[i][j]=m+n;
}
if(i>0){
grid[i][j]=Math.min(grid[i][j],grid[i-1][j]+1);
}
if(j>0){
grid[i][j]=Math.min(grid[i][j],grid[i][j-1]+1);
}
}
} int res=0;
for(int i=m-1;i>=0;i--){
for(int j=n-1;j>=0;j--){
if(grid[i][j]==1){
continue;
} if(i<m-1){
grid[i][j]=Math.min(grid[i][j],grid[i+1][j]+1);
}
if(j<n-1){
grid[i][j]=Math.min(grid[i][j],grid[i][j+1]+1);
}
//他需要的是最远的距离中最近的
res=Math.max(res,grid[i][j]);
}
} return f?res-1:-1;
}
}

Java实现 LeetCode 1162 地图分析(可以暴力或者动态规划的BFS)的更多相关文章

  1. Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible)

    Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. ...

  2. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  4. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. X Error:BadDrawable (individ Pixmap or Window parameter 9)

    #描述 平台:aarch64 系统:ubuntu16.04.02 Qt Version:4.8.7 Qt程序可以正常运行,界面渲染出现问题以及乱码,控制提示错误内容: "unable to ...

  2. 《C程序设计语言》 练习2-8

    问题描述 练习 2-8 编写一个函数rightrot(x, n),该函数返回将x循环右移(即从最右端移出的位将从最左端移入)n(二进制)位后所得到的值. Write a function rightr ...

  3. Redis 6.0 多线程重磅发布!!!

    Redis 6.0在5.2号这个美好的日子里悄无声息的发布了,这次发布在IT圈犹如一颗惊雷一般,因为这是redis最大的一次改版,首次加入了多线程. 作者Antirez在RC1版本发布时在他的博客写下 ...

  4. 黑马程序员_毕向东_Java基础视频教程——位运算符(随笔)

    位运算符 左移和右移 左移 左移越移越大. 往左移几位就相当于这个数乘于2的几次方 3 << 2 --> 3 * 2^2 = 3 * 4 = 12 3 << 3 --&g ...

  5. linux下安装gmp遇到 configure:error:no usable m4 in$path or /user/5bin解决方案

    安装过程中遇到如下报错: 上面的报错是因为你没有安装m4,安装m4就可以了:以下两种命令人选其一: #yum install m4 或 #apt-get install m4 ps:如果遇到权限问题就 ...

  6. quartzJob

    定时任务的时间修改.暂停.立即执行 定时任务的修改.暂停主要是调用quartz内置方法pauseJob().resumeJob().triggerJob()等方法 //暂停一个job JobKey j ...

  7. fetch方法

    在<深入浅出React和Redux>一书中,介绍react与服务器端交互时,用了fetch方法:https://github.com/github/fetch.该网址上有各种使用例子. 安 ...

  8. 第七篇:wed版语音机器人

    wed版语音机器人: GitHub项目地址:https://github.com/Yang915/WebToy 特别说明:该项目在本机测试,通过浏览器调用系统麦克风(https请求),实际环境在Fir ...

  9. ABAP基础3:OPENSQL

    select result from source where condition group by fileds having condition order by fields into targ ...

  10. HTML使用正则验证

    制作HTML前台用户验证等,需要对用户名或者密码进行验证,这时使用正则表达式能够精确地对text进行限制. 具体在HTML中的运用代码如下: 转自 https://blog.csdn.net/weix ...