Java实现 LeetCode 1162 地图分析(可以暴力或者动态规划的BFS)
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)的更多相关文章
- Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible)
Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. ...
- 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 ...
- Java for LeetCode 044 Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【Hadoop离线基础总结】oozie任务串联
目录 需求 1.准备工作目录 2.准备调度文件 3.开发调度的配置文件 4.上传资源文件夹到hdfs对应路径 5.执行调度任务 需求 执行shell脚本 → 执行MR程序 → 执行hive程序 1.准 ...
- 明解JAVA 第三章答案
练习3-1 package candle1220; import java.util.Scanner; public class Nightwatch { public static void mai ...
- [hdu5358]分类统计,利用单调性优化
题意:直接来链接吧http://acm.hdu.edu.cn/showproblem.php?pid=5358 思路:注意S(i,j)具有区间连续性且单调,而⌊log2x⌋具有区间不变性,于是考虑枚举 ...
- 配置centos7 java环境
一.环境 centos7 jdk-8u231-linux-x64.tar.gz 二.安装jdk 使用ftp或者 WinScp软件把下载在win10电脑上的jdk安装包上传到linux 解压到/opt/ ...
- dede文章列表根据权重自定义排序
首先在这里说明一下,网上搜索织梦文章列表自定义的方式无非有以下几种: 1.修改配置文件,新增一个orderid字段,想法很好,但是!但是!!!可能是早期版本的缘故,不适合现在的v5.7系统!按照网上说 ...
- 51Nod1127 最小包含字符串
51Nod1127 #include <iostream> #include <string> using namespace std; const int inf = 0x3 ...
- SpringBoot整合Redis实现简单的set、get
一.导入pom.xml文件相关的依赖并配置 <dependency> <groupId>org.springframework.boot</groupId> < ...
- IntelliJ IDEA 2020.1 取消了auto-import自动导入
Maven 和 Gradle 导入功能更新 v2020.1使得Maven和Gradle更改的导入不再繁琐.首先,我们删除了总是触发的自动导入,以及在更新完脚本之前不断显示并建议导入更新的提示框.取而代 ...
- 苏浪浪 201771010120 《面向对象程序设计(java)》第七章学习总结
第七周 1.实验目的与要求 (1)进一步理解4个成员访问权限修饰符的用途: (2)掌握Object类的常用API用法: (3)掌握ArrayList类用法与常用API: (4)掌握枚举类使用方法: ( ...
- 使用容器化块存储OpenEBS在K3s中实现持久化存储
作者简介 Giridhara Prasad,Mayadata Inc.首席工程师.在软件测试自动化.混沌工程(chaos engineering)方面有丰富的经验.目前,他正在研究开源混沌工程项目Li ...