"""
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的更多相关文章

  1. POJ 1365 Prime Land(数论)

    题目链接: 传送门 Prime Land Time Limit: 1000MS     Memory Limit: 10000K Description Everybody in the Prime ...

  2. [2015hdu多校联赛补题]hdu5378 Leader in Tree Land

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...

  3. UVALive 7261 Xiongnu's Land (扫描线)

    Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against the ...

  4. hdu----(5050)Divided Land(二进制求最大公约数)

    Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. (UVALive 7261)Xiongnu's Land 二分

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

随机推荐

  1. tomcat启动报错failed to start component

    严重: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catali ...

  2. Linux软Raid--mdadm命令

    mdadm:为软RAID提供管理界面,RAID设备可命名为/dev/md0./dev/md1./dev/md2./dev/md3等 命令的语法格式:mdadm[mode] <raiddevice ...

  3. nacos 日志问题 ERR-CODE: [NACOS-0002], Type: [环境问题]

    nacos配置中心配置后,项目启动正常,运行项目也正常,但是总是打印如下日志: 2019-10-11 15:44:09.792 [com.alibaba.nacos.client.Worker.lon ...

  4. Linux centos VMware Nginx防盗链、Nginx访问控制、Nginx解析php相关配置、Nginx代理

    一.Nginx防盗链 配置如下,可以和上面的配置结合起来 location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|x ...

  5. 「SPOJ10707」Count on a tree II

    「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:复选框(Checkbox)和单选框(Radio)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-warning" 类的文本样式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. Netty实现原理和使用

    参考: https://www.jdon.com/concurrent/netty.html Java NIO原理和使用 参考:https://www.jdon.com/concurrent/nio% ...

  9. RCTF crypto100(1)

    题面是一个文件,里面是用base64加密的字符串,如下所示: IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjI ...

  10. Android View转换成图片保存

    package zhangphil.viewtoimage; import java.io.File;import java.io.FileOutputStream; import android.o ...