EPS/2024/01/29-01-24/king_escape.py

48 lines
1.1 KiB
Python
Raw Permalink Normal View History

2024-01-30 15:04:05 +01:00
import heapq
def is_blocked(d, ax, ay):
dx, dy = d
if dx == ax or dy == ay:
return True
if abs(dx-ax) == abs(dy-ay):
return True
return False
def valid(v, n):
return v[0] > 0 and v[1] > 0 and v[0] <= n and v[1] <= n
def dist(v, cx, cy):
return abs(v[0]-cx)**2+abs(v[1]-cy)**2
def main():
n = int(input())
ax, ay = [int(i) for i in input().split()] # queen
bx, by = [int(i) for i in input().split()] # king
cx, cy = [int(i) for i in input().split()] # dest
views = {(bx, by): 0}
queue = [(dist((bx, by), cx, cy), (bx, by))]
while queue:
prior, elem = heapq.heappop(queue)
if elem == (cx, cy):
print("YES")
return
voisins = []
for i in range(-1, 2):
for j in range(-1, 2):
v = (elem[0]+i, elem[1]+j)
if valid(v, n) and v not in views.keys():
voisins.append(v)
views.append(v)
for v in voisins:
if not is_blocked(v, ax, ay):
heapq.heappush(queue, (dist(v, cx, cy), v))
print("NO")
main()