classSolution: defisEscapePossible(self, blocked, source, target) -> bool: m = 10 ** 6 hashBlock = dict(map(lambda x: [tuple(x), 0], blocked)) queue_s = [tuple(source)] queue_t = [tuple(target)] hashPoint_s = dict() hashPoint_t = dict() hashPoint_s[tuple(source)] = 0 hashPoint_t[tuple(target)] = 0 direct = [(-1, 0), (1, 0), (0, -1), (0, 1)] while queue_s != [] and queue_t != []: len_s = len(queue_s) len_t = len(queue_t) if len_s < len_t: for i inrange(len_s): point = queue_s.pop(0) for d in direct: new_point = (point[0] + d[0], point[1] + d[1]) if new_point[0] < 0or new_point[0] >= m or new_point[1] < 0or new_point[1] >= m: continue if new_point in hashBlock: continue if new_point in hashPoint_s: continue if new_point in hashPoint_t: returnTrue queue_s.append(new_point) hashPoint_s[new_point] = 0 else: for i inrange(len_t): point = queue_t.pop(0) for d in direct: new_point = (point[0] + d[0], point[1] + d[1]) if new_point[0] < 0or new_point[0] >= m or new_point[1] < 0or new_point[1] >= m: continue if new_point in hashBlock: continue if new_point in hashPoint_t: continue if new_point in hashPoint_s: returnTrue queue_t.append(new_point) hashPoint_t[new_point] = 0 returnFalse
classSolution: defisEscapePossible(self, blocked, source, target) -> bool: m = 10 ** 6 hashBlock = dict(map(lambda x: [tuple(x), 0], blocked)) n = len(hashBlock) n = n*(n-1)//2# 计算可封锁的点的最大数量 queue_s = [tuple(source)] queue_t = [tuple(target)] hashPoint_s = dict() hashPoint_t = dict() hashPoint_s[tuple(source)] = 0 hashPoint_t[tuple(target)] = 0 direct = [(-1, 0), (1, 0), (0, -1), (0, 1)] while queue_s != [] and queue_t != []: len_s = len(queue_s) len_t = len(queue_t) ifmin(len(hashPoint_s),len(hashPoint_t))>n: # 判断是否可以冲破封锁 returnTrue if len_s < len_t: for i inrange(len_s): point = queue_s.pop(0) for d in direct: new_point = (point[0] + d[0], point[1] + d[1]) if new_point[0] < 0or new_point[0] >= m or new_point[1] < 0or new_point[1] >= m: continue if new_point in hashBlock: continue if new_point in hashPoint_s: continue if new_point in hashPoint_t: returnTrue queue_s.append(new_point) hashPoint_s[new_point] = 0 else: for i inrange(len_t): point = queue_t.pop(0) for d in direct: new_point = (point[0] + d[0], point[1] + d[1]) if new_point[0] < 0or new_point[0] >= m or new_point[1] < 0or new_point[1] >= m: continue if new_point in hashBlock: continue if new_point in hashPoint_t: continue if new_point in hashPoint_s: returnTrue queue_t.append(new_point) hashPoint_t[new_point] = 0 returnFalse