leetcode1162 As Far from Land as Possible
"""
Given an N x N grid containing only values and , where represents water and represents land, find a water cell such that its distance to the nearest land cell is maximized and return the distance.
The distance used in this problem is the Manhattan distance: the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|.
If no land or water exists in the grid, return -.
Example :
Input: [[,,],[,,],[,,]]
Output:
Explanation:
The cell (, ) is as far as possible from all the land with distance .
Example :
Input: [[,,],[,,],[,,]]
Output:
Explanation:
The cell (, ) is as far as possible from all the land with distance .
"""
"""
BFS的方法
先找到grid中所有的1的位置,保存在queue中,若全是1或者没有1,返回-
从这些1所在的位置开始进行广度优先搜索,即搜索四个方向,
如果搜索的位置上的值为0,保存这些坐标,
作为下一次搜索的起点,并将这些位置的值设置为1,以免重复搜索。
这样每扩散一层,计数器加1,直到没有可搜索的起点,返回计数器的值就为最远的距离
"""
class Solution:
def maxDistance(self, grid):
row, col = len(grid), len(grid[]) #求出地图的行列
queue = [] #保存地图中出现''的位置
for i in range(row): #遍历地图。将为''的所有位置放入队列
for j in range(col):
if grid[i][j] == :
queue.append((i, j))
if len(queue) == row*col or not queue: #如果地图全为''或者全为'',返回-
return -
level = #记录每次扩张的距离
while queue:
newqueue = [] #记录每层扩张
for x, y in queue:
for i, j in [[x-, y], [x+, y], [x, y-], [x, y+]]: #每层扩张的四个方向
if <= i <row and <= j < col and grid[i][j] == :
newqueue.append((i, j)) #如果为0,搜索到了,保存位置放入队列,下一轮搜索
grid[i][j] = #并将搜索过的位置 变为1
queue = newqueue
level +=
return (level-)
leetcode1162 As Far from Land as Possible的更多相关文章
- POJ 1365 Prime Land(数论)
题目链接: 传送门 Prime Land Time Limit: 1000MS Memory Limit: 10000K Description Everybody in the Prime ...
- [2015hdu多校联赛补题]hdu5378 Leader in Tree Land
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...
- UVALive 7261 Xiongnu's Land (扫描线)
Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against the ...
- hdu----(5050)Divided Land(二进制求最大公约数)
Divided Land Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1507 Uncle Tom's Inherited Land*(二分图匹配)
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力
A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/cont ...
- (UVALive 7261)Xiongnu's Land 二分
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
随机推荐
- python开发接口
享一段代码,开发了3个接口: 1.上传文件 2.查看所有文件 3.下载文件 使用python开发,需要安装flask模块,使用pip ...
- 支持USB4的Linux 5.6,有望在今年4月份推出
导读 根据外媒Phoronix的报道,Linux 5.6将支持USB4,. USB4的规范在去年9月份发布,基于雷电3,并与之向后兼容.英特尔的开源部门在去年10月份添加了USB4的初始补丁. 据报道 ...
- 修改vue中的挂载页面(index.html)的路径
修改vue中的挂载页面(index.html)的路径 2019年03月30日 12:07:12 VegasLemon 阅读数 501 版权声明:本文为博主原创文章,未经博主允许不得转载. htt ...
- 分布式事务中间件 TCC-Transaction 源码分析 —— 项目实战
https://blog.csdn.net/lldouble/article/details/79455172
- YARN 集群的资源分配
YARN 集群在分配任务时,存在两种方式 1. DefaultResourceCalculator,只考虑内存(memory),每个 container 的 cpu 资源都分配 1 个. 2. Dom ...
- Lesson 12 banks and their customers
Why is there no risk to the customer when a bank prints the customer's name on his cheques? When any ...
- 网络协议-restful协议
REST Representational State Transfer, 是一种软件架构风格,提供一系列限制指导,用于更好的创建web service. 符合REST 架构风格的web servic ...
- Acwing779 最长公共字符串后缀
题目大意:给定n个字符串,让你找到他们的最长公共字符串后缀是什么,可能为空. 分析:题目数据范围比较小,可以O(n*n)暴力匹配,即可解决这道问题.之所以写这道题的题解还是因为写字符串的题还不够多啊, ...
- sdfsdsf
1 $('.advert-title').each(function(){ 2 var TXTlength = $(this).text().length; // 当前文本的长度 3 if(TXTle ...
- Python 100 Days
Day 1 python的缺点 执行效率稍低,因此计算密集型任务可以由C/C++编写. 在开发时可以选择的框架太多(如Web框架就有100多个),有选择的地方就有错误. python解释器 官方的Py ...