18_memspace.py 967 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env python3
  2. import sys
  3. from heapq import heappop, heappush
  4. def shortest_path(falls):
  5. work = [(0, ((0, 0),))]
  6. dist = {(0, 0): 0}
  7. while work:
  8. length, path = heappop(work)
  9. x, y = path[-1]
  10. if dist[x, y] != length:
  11. continue
  12. if x == 70 and y == 70:
  13. return set(path)
  14. for nb in ((x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)):
  15. nx, ny = nb
  16. if 0 <= nx <= 70 and 0 <= ny <= 70 and nb not in falls and \
  17. dist.get(nb, 1e4) > length + 1:
  18. dist[nb] = length + 1
  19. heappush(work, (length + 1, path + (nb,)))
  20. falls = [tuple(map(int, line.split(','))) for line in sys.stdin]
  21. cur = set(falls[:1024])
  22. path = shortest_path(cur)
  23. print(len(path) - 1)
  24. for fall in falls[1024:]:
  25. cur.add(fall)
  26. if fall in path:
  27. path = shortest_path(cur)
  28. if path is None:
  29. print('%d,%d' % fall)
  30. break