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 ...
随机推荐
- CF#135 D. Choosing Capital for Treeland 树形DP
D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...
- [csu/coj 1632]LCP
题意:求一个串的出现次数超过1次的字串的个数 思路:对于一个后缀,出现在它后面的所有后缀与它的LCP的最大值就是应该增加的答案,当然这里没有考虑去重,但是却转化了问题,使得我们可以用最长公共前缀来统计 ...
- 关于fromdata的上传文件问题
<div <label>上传pdf</label> <input id="fileId" type="file" accep ...
- ZOOM火速收购加密公司Kaybase 能否补齐安全短板?
近日,一直因为安全漏洞饱受批评的云视频会议协作工具ZOOM,,其公司全资收购一家初创企业Kaybase,以加强ZOOM系统的隐私保护和安全性. Kaybase公司官网 2020年年初,随着疫情的蔓 ...
- 马上要去bupt参加培训了。。
想想我一直在逃避,失败人生.傻逼废物垃圾废人...我这么弱还活在世界上,真的对不起 Model
- 关于redis内存分析,内存优化
对于redis来说,什么是最重要的? 毋庸置疑,是内存. 一.reids 内存分析 redis内存使用情况:info memory 示例: 可以看到,当前节点内存碎片率为226893824/20952 ...
- SDK,JDK,API的区别
[基础概念] 先留一波传送门: SDK:软件开发工具包(外语全称:Software Development Kit)一般都是一些软件工程师为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件时的 ...
- zsteg
以前知道zsteg,但是没有去安装使用,所以就一直没有使用. 最近在找有关lsb隐写的题,发现有些大佬的wp上面写用zsteg就解决了lsb隐写的题,自己就想,难道zsteg很方便,于是就去安装zst ...
- OpenCV开发笔记(五十六):红胖子8分钟带你深入了解多种图形拟合逼近轮廓(图文并茂+浅显易懂+程序源码)
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- Docker 集成 Jenkins Gitlab 实现 CI/CD
首先介绍下环境部分,文章中共涉及到三台服务器,分别用 Gitlab,Jenkins,Deploy 三个名称代替,部署在内网环境,同时因为政策原因,服务器无法直接连通外网.下载 Jenkins 插件时需 ...