题目如下:

解题思路:本题题干中提到了一个非常重要的前提:"You can swim infinite distance in zero time",同时也给了一个干扰条件,那就是示例2里面的说明,"We need to wait until time 16 so that (0, 0) and (4, 4) are connected."。那么,游泳的人是否需要先游到(1,4)这个点然后等待到time16呢?把问题简化一下,游泳者可以直接在(0,0)等待到time16,这样问题就变成了在什么时间点,游泳的人可以从起点游到终点。在某一特定时间点,矩阵中所有大于这个时间的元素认为是障碍,这样是不是就是一个迷宫问题了?至于怎么找到最小的时间点,首先可以确定,最大的时间是矩阵中值最大的元素,最小的时间是起点元素和终点元素的较大值。显然,用二分查找非常合适。

代码如下:

class Solution(object):
def canReach(self,time,matrix):
tl = [0] * len(matrix[0])
visit = []
for i in matrix:
visit.append(tl[:])
row = len(matrix)
column = len(matrix[0])
queue = [(0,0)]
visit[0][0] = 1
while len(queue) > 0:
x,y = queue.pop(0)
#print x,y
if x == row-1 and y == column-1:
return True
if x - 1 >= 0 and matrix[x-1][y] <= time and visit[x-1][y] == 0:
queue.append((x-1,y))
visit[x-1][y] = 1
if x + 1 < row and matrix[x+1][y] <= time and visit[x+1][y] == 0:
queue.append((x+1,y))
visit[x+1][y] = 1
if y - 1 >= 0 and matrix[x][y-1] <= time and visit[x][y-1] == 0:
queue.append((x,y-1))
visit[x][y-1] = 1
if y + 1 < column and matrix[x][y+1] <= time and visit[x][y+1] == 0:
queue.append((x,y+1))
visit[x][y+1] = 1
return False def swimInWater(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
low = max(grid[0][0],grid[-1][-1])
high = low
for i in grid:
for j in i:
high = max(j,high)
res = len(grid)*len(grid)
while low <= high:
mid = (low + high) / 2
if self.canReach(mid,grid) == True:
res = min(res,mid)
high = mid - 1
else:
low = mid + 1
return res

【leetcode】778. Swim in Rising Water的更多相关文章

  1. 【LeetCode】778. Swim in Rising Water 水位上升的泳池中游泳(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/swim-in- ...

  2. LeetCode 778. Swim in Rising Water

    题目链接:https://leetcode.com/problems/swim-in-rising-water/ 题意:已知一个n*n的网格,初始时的位置为(0,0),目标位置为(n-1,n-1),且 ...

  3. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  4. 【LeetCode】11. Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  5. 778. Swim in Rising Water

    ▶ 给定方阵 grid,其元素的值为 D0n-1,代表网格中该点处的高度.现在网格中开始积水,时刻 t 的时候所有值不大于 t 的格点被水淹没,当两个相邻格点(上下左右四个方向)的值都不超过 t 的时 ...

  6. 【LeetCode】011 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  7. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  8. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  9. 【LeetCode】并查集 union-find(共16题)

    链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence  (2018年11月22日,开始解决hard题) 给 ...

随机推荐

  1. Ta还没有分享呢,过段时间再来看看吧~ 解决办法

    自己摸索出来的.只能查看以前分享的奥.   找到要查看用户的id号   利用特百度搜索工具实现检索 http://www.tebaidu.com/user--1.html 将红字部分替换为刚才复制的u ...

  2. 词频分析 评论标签 nltp APP-分析买家评论的评分-高频词:二维关系

    0-定评论结果:好评.差评,1星.4星,二元化为“积极.消极”,取一元的数据为样本 1-得到词频结果:如手机类的“积极样本”得到前10的高频词:运行(run running ran).内存(memor ...

  3. day63—JavaScript浏览器对象cookie

    转行学开发,代码100天——2018-05-18 今天的主要学内容时JavaScript中浏览器对象——cookie. cookie用于存储web页面的用户信息,其存储容量很小,一般几k左右.如常见的 ...

  4. MVC3: 页面向服务传参(view->controller,get,post)

    HttpGet HttpPost GetPost 总结比较 ajax get post 1)HttpGet Get是http请求的默认形式.拿MVC官方教程Movie项目举例如下. 编辑页面  Vie ...

  5. 腾讯重磅开源分布式NoSQL存储系统DCache

    当你在电商平台秒杀商品或者在社交网络刷热门话题的时候,可以很明显感受到当前网络数据流量的恐怖,几十万商品刚开抢,一秒都不到就售罄:哪个大明星出轨的消息一出现,瞬间阅读与转发次数可以达到上亿.作为终端用 ...

  6. C#网络编程 多线程和高并发

    在任何 TCP Server 的实现中,一定存在一个 Accept Socket Loop,用于接收 Client 端的 Connect 请求以建立 TCP Connection. 在任何 TCP S ...

  7. 域名 端口 DNs 网络

    netstat -an   查看所有 所有监听端口的使用情况

  8. 《剑指offer》面试题13 在O(1)时间删除链表节点 Java版

    这道题的关键是知道找到尾节点的前一个节点必须遍历,而且这样做了之后总的时间复杂度还是O(1),以及如何不破坏链表删除一个已知节点 public ListNode delete(ListNode hea ...

  9. exec 命令

    source source命令即点(.)命令. 在bash下输入man source,找到source命令解释处,可以看到解释”Read and execute commands from filen ...

  10. 深入ArrayList看fast-fail机制

    fail-fast机制简介 什么是fail-fast fail-fast 机制是java集合(Collection)中的一种错误机制.它只能被用来检测错误,因为JDK并不保证fail-fast机制一定 ...