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_ ...
随机推荐
- Atom 必装插件
Atom 必装插件 转载请注明出处. https://blog.csdn.net/Nick_php/article/details/54020956 主题 atom-material-ui 字体配色 ...
- Linux环境查看Java应用消耗资源情况
linux线上资源耗时定位 https://www.cnblogs.com/wuchanming/p/7766994.html 1. jps -ml 查看服务器上运行的Java程序 2. jmap 查 ...
- Duilib定时器
转载:https://www.zhaokeli.com/article/8262.html 问题描述 直接使用系统函数定义 参数 http://www.zhaokeli.com/article/18 ...
- C++11通过拷贝构造器拷贝const字段(待解决)
问题梗概如标题所述. 在今天实现Token类的时候,遇到的问题. 我希望将Token类设定为immutable属性的,这样实现的方式是将这个类内的所有字段均设置为const型,同时每个字段均为publ ...
- while (rs.next()) 与 if(rs.next())的区别
while (rs.next())是用来循环遍历结果集的. if(rs.next())是用来判断结果集是否有值,有值则执行if语句内代码块. 简而言之 while(rs.next()) 就是将rs全部 ...
- VS2010如何进行程序调试
VS2010如何进行程序调试 一.前言 对于初步学习C++的朋友,程序的调试是一项必备的技能.尤其是像C++这样难学的语言,程序调试的基本方法更是至关重要.毕竟,谁也不想自己幸幸苦苦一天敲出来的代码就 ...
- 白底黑字!Android浅色状态栏黑色字体模式(另)
小彬什么都想做任重致远 关注 2016.06.30 10:16* 字数 489 阅读 3234评论 3喜欢 12 前言 由于该死不死的设计湿,设计了一套白色状态栏的UI.当然在iOS上可以实现自适应, ...
- 如何知道某个ACTIVITY是否在前台?
本文链接:http://zengrong.net/post/1680.htm 有一个Android应用包含包含一个后台程序,该程序会定期连接服务器来实现自定义信息的推送.但是,当这个应用处于前台的时候 ...
- DatePicker和DatePickerDialog的使用
activity_main.xml: <Button android:id="@+id/showDatePickerButton" android:text="@s ...
- 剑指offer 二叉树的后序遍历序列
题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路:这题目有点特殊,主要在于序列为空的时候,不是 ...