代码拉取完成,页面将自动刷新
# Time: O(n^2), n is the number of blocked
# Space: O(n)
import collections
class Solution(object):
def isEscapePossible(self, blocked, source, target):
"""
:type blocked: List[List[int]]
:type source: List[int]
:type target: List[int]
:rtype: bool
"""
R, C = 10**6, 10**6
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
def bfs(blocks, source, target):
max_area_surrounded_by_blocks = len(blocks)*(len(blocks)-1)//2
lookup = set([source])
if len(lookup) > max_area_surrounded_by_blocks:
return True
q = collections.deque([source])
while q:
source = q.popleft()
if source == target:
return True
for direction in directions:
nr, nc = source[0]+direction[0], source[1]+direction[1]
if not ((0 <= nr < R) and
(0 <= nc < C) and
(nr, nc) not in lookup and
(nr, nc) not in blocks):
continue
lookup.add((nr, nc))
if len(lookup) > max_area_surrounded_by_blocks:
return True
q.append((nr, nc))
return False
return bfs(set(map(tuple, blocked)), tuple(source), tuple(target)) and \
bfs(set(map(tuple, blocked)), tuple(target), tuple(source))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。