"""
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. 计算机网络历史与基本概念&分层与参考模型(TCP/IP与OSI)&通信过程

    Definition: 计算机网络:使用单一技术相互连接的自主计算机的互联集合. 单台计算机独立自主(不受制于其他计算机),连接介质可以使光纤.铜线也可以是微波.红外.卫星. 互联网络(Interne ...

  2. 从零构建以太坊(Ethereum)智能合约到项目实战——第23章 从零构建和部署去中心化投票App,decentralization Voting Dapp

    P90 .1-从零构建和部署去中心化投票App-01 P91 .2-从零构建和部署去中心化投票App-02 P92 .3-从零构建和部署去中心化投票App-03 参考博文:http://liyuech ...

  3. (转)js实现倒计时效果(年月日时分秒)

    原文链接:http://mengqing.org/archives/js-countdown.html 之前做的活动页面很多都用到了倒计时功能,所以整理下下次直接用.(用的是张鑫旭写的一个倒计时,稍作 ...

  4. 「CH6901」骑士放置

    「CH6901」骑士放置 传送门 将棋盘黑白染色,发现"日"字的两个顶点刚好一黑一白,构成一张二分图. 那么我们将黑点向源点连边,白点向汇点连边,不能同时选的一对黑.白点连边. 当 ...

  5. Centos7 iptables firewalld防火墙与selinux配置

    一.iptables防火墙 1.基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service ...

  6. Derivative Pricing_1_Black

    1. Stock Option wih divends 1.1 Task A 1.1.1 Calculate a ECO on a stock. /Ex-dividend dates in 3 and ...

  7. FFmpeg调用c语言SDK实现日志的打印

    日志文件的三大步 // 导入头文件 #include <libavutil/log.h> // 设置日志级别 av_log_set_level(AV_LOG_DEBUG); //DEBUG ...

  8. 按钮UIButton的使用

    一.使用概要 当添加一个按钮到你的界面,执行以下步骤: 1.在创建时设置按钮的类型. 2.提供一个标题字符串或图像,为您的内容适当调整按钮的大小. 3.连接一个或多个操作按钮的方法. 4.设置自动布局 ...

  9. Linux centos7 日常运维——使用w查看系统负载、vmstat命令、top命令、sar命令、nload命令

    一.使用w查看系统负载 w .uptime查看系统负载,0.00表示1分钟之内负载为0 cat  /proc/cpuinfo查看cpu核数 二.vmstat命令,查看进程.cpu.memory.交换. ...

  10. PHP 符号大全

    注解符号: // 单行注解 /*      */    多行注解 引号的使用 ’   ’ 单引号,没有任何意义,不经任何处理直接拿过来; " "双引号,PHP动态处理然后输出,一般 ...