"""
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. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

    1007 Maximum Subsequence Sum (25分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  2. 06. Z字型变换

    题目: 提交01: class Solution { public String convert(String s, int numRows) { int length = 2*numRows-2; ...

  3. JS - 处理浏览器兼容之 event

    function test(e){ var event = e || windows.event   //  IE : windows.event  ,非IE : e }

  4. Linux centosVMware apache 限定某个目录禁止解析php、限制user_agent、php相关配置

    一.限定某个目录禁止解析php 核心配置文件内容 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 先创建.编辑一个php 配置 vim /u ...

  5. CSP2019 括号树

    Description: 给定括号树,每个节点都是 ( 或 ) ,定义节点的权值为根到该节点的简单路径所构成的括号序列中不同合法子串的个数(子串需要连续,子串所在的位置不同即为不同.)与节点编号的乘积 ...

  6. 108、Java中String类之字符串文本替换

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  7. ws2_32.dll的妙用与删除 (禁网)

    ws2_32.dll是Windows Sockets应用程序接口,用于支持Internet和网络应用程序.程序运行时会自动调用ws2_32.dll文件,ws2_32.dll是个动态链接库文件位于系统文 ...

  8. 利用TPL(任务并行库)构建Pipeline处理Dataflow

    https://www.cnblogs.com/CoderAyu/p/9757389.html

  9. python对ASC码的加减

    一般使用这两个函数 sum = ord('A') //结果为65 ord()函数返回值是int 字符要加' '否则会当作变量来看 sum = chr(65) //结果为A 不是char是chr()

  10. 视频游戏的连击 [USACO12JAN](AC自动机+动态规划)

    传送门 默认大家都学过trie与AC自动机. 先求出fail,对于每个节点维护一个sum,sum[u]待表从根到u所形成的字符串能拿到几分.显然sum[u]=sum[fail] + (u是几个字符串的 ...