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. 如何为Linux服务器添加磁盘

    Linux服务器如果磁盘不够用了,就需要增加新的磁盘,磁盘添加到使用通常有4个步骤.其中第一个步骤虚拟机和实体服务器有差别,后面三个步骤都是相同的,这里以VMWare虚拟机来进行演示如何添加磁盘. ( ...

  2. 不同版本(2.3/2.4/2.5/3.0/3.1)web.xml头信息

    Web App 3.1 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http:// ...

  3. HttpPoolUtils 连接池管理的GET POST请求

    package com.nextjoy.projects.usercenter.util.http; import org.apache.http.Consts; import org.apache. ...

  4. .Net Core3.0 WebApi 项目框架搭建 三:读取appsettings.json

    .Net Core3.0 WebApi 项目框架搭建:目录 appsettings.json 我们在写项目时往往会把一些经常变动的,可能会变动的参数写到配置文件.数据库中等可以存储数据且方便配置的地方 ...

  5. percona 5.6的安装

    yum 安装, 1 如果已经安装过mysql 的东西,先卸载了.yum remove mysql* 包括 /etc/my.cnf 这个东西卸载的时候不会删除. mv /etc/my.cnf /etc/ ...

  6. MySQL数据库基础操作语句

    SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,分为3种类型: 1.DDL语句 数据库定义语言: 数据库.表.视图.索引.存储过程,例如CREATE DROP ALTER 2.DM ...

  7. Django模板之模板组件(include)

    可以将常用的页面内容如导航条,页尾信息等组件保存在单独的文件中的一个小功能块,然后在需要使用的地方,文件的任意位置按如下语法导入即可. 模板组件: 新建一个组件zujian.html文件(一个固定写好 ...

  8. uefi win10 Ubuntu 18的安装

    uefi win10 Ubuntu 18的安装 (Ubuntu折腾的第一天) 安装时的踩坑记录

  9. JavaScript数组常见用法

    最近做一个项目中做一个竞猜游戏界面,游戏规则和彩票是一样的.在实现“机选一注”,“机选五注”的时候遇到数组的一些操作,例如产生['01', '02' ... '35']这样的数组,随机抽取不重复的元素 ...

  10. 一次 nginx 返回 302 问题解决

    1.问题描述: 应用服务器通过post方式向nginx服务器发送http请求,返回 302 2.问题解决过程 2.1.查询nginx日志,开始以为302错误会在nginx的错误日志error.log, ...