题目如下:

Given an N x N grid containing only values 0 and 1, where 0 represents water and 1 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 -1.

Example 1:

Input: [[1,0,1],[0,0,0],[1,0,1]]
Output: 2
Explanation:
The cell (1, 1) is as far as possible from all the land with distance 2.

Example 2:

Input: [[1,0,0],[0,0,0],[0,0,0]]
Output: 4
Explanation:
The cell (2, 2) is as far as possible from all the land with distance 4.

Note:

  1. 1 <= grid.length == grid[0].length <= 100
  2. grid[i][j] is 0 or 1

解题思路:题目不难,注意用BFS,如果用DFS会超时。

代码如下:

class Solution(object):
def maxDistance(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
val = [[float('inf')] * len(grid[0]) for _ in grid]
queue = [] for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
queue.append((i, j, 0)) direction = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while len(queue) > 0:
x, y, dis = queue.pop(0)
for (x1, y1) in direction:
if x + x1 >= 0 and x + x1 < len(grid) and y + y1 >= 0 \
and y + y1 < len(grid[0]) and grid[x + x1][y + y1] == 0 \
and val[x + x1][y + y1] > dis + 1:
val[x + x1][y + y1] = dis + 1
queue.append((x + x1, y + y1, dis + 1)) res = -1
for i in range(len(val)):
for j in range(len(val[i])):
if val[i][j] != float('inf') and res < val[i][j]:
res = val[i][j] return res

【leetcode】1162. As Far from Land as Possible的更多相关文章

  1. 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...

  2. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. Eclipse设置保存时自动格式化代码

    在使用eclipse时,经常需要使用到Ctrl+Shift+F来格式化代码,可以打开保存时格式化,会更方便. 打开方式:Window-->Preferences-->Java --> ...

  2. 深入理解java:1.3. 垃圾收集

    Java垃圾收集(Garbage Collection,GC) 某一个时点,一个对象如果有一个以上的引用(Rreference)指向它,那么该对象就为活着的(Live), 否则死亡(Dead),视为垃 ...

  3. PostgreSQL索引思考

    当在看Monetdb列存行只支持IMPRINTS和ORDERED这两种索引,且只支持定长数值类型时,就在思考,对于列存,还有必要建索引吗?在PostgreSQL的索引就要灵活很多,我对常用列建合理的索 ...

  4. POJ 3743 LL’s cake(圆+PSLG)

    题意是给你一块在原点半径为10的圆,然后告诉你一条直线在圆弧上的极角,相当于用这条直线把这个圆分成两半,然后一共是n条直线切圆,就好比切蛋糕,问你其中最大一块的面积是多少. 如果我们将圆弧转化成直线边 ...

  5. Python作图包含type3字体解决方案

    1. 解决方案 matplotlib.rcParams[‘text.usetex’] = True

  6. 对于出现拒绝访问root用户的解决方案

    提示:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'   由于使用mysql -u root ...

  7. hihocoder 1636 : Pangu and Stones(区间dp)

    Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the first livi ...

  8. sql 字符串 切割函数 FUN_Split

    IF EXISTS ( SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[FUN_Split]') AND OBJECTPROPER ...

  9. C# 静态方法调用非静态方法

    转载:http://blog.csdn.net/seattle1215/article/details/6657814 using System; using System.Collections.G ...

  10. CTS2018+1没去记&APIO2018+1游记

    bgm Tips:想知道为什么标题不是2019吗,请看评论区 Day -2 md上午才写一道题qwq 机房里一堆dalao都回家收东西去北京了qwq,我菜死了,cts都去不了QAQ 下午晚上也才肝了两 ...