namemkazaza

R

Nov 28th, 2020
727
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with open("input.txt", mode="r") as f_read:
  2.     n, m = map(int, f_read.readline().strip().split())
  3.     x_start, y_start = map(int, f_read.readline().strip().split())
  4.     x_finish, y_finish = map(int, f_read.readline().strip().split())
  5.     A = [list(i.strip()) for i in f_read.readlines()]
  6.  
  7. x_finish -= 1
  8. y_finish -= 1
  9.  
  10. queue = [(x_start - 1, y_start - 1, "0")]
  11.  
  12. while len(queue) > 0:
  13.     x, y, sub = queue.pop(0)
  14.     A[x][y] = sub
  15.     if (x, y) == (x_finish, y_finish):
  16.         break
  17.     if x > 0 and A[x - 1][y] == ".":
  18.         queue.append((x - 1, y, str(int(sub)+1)))
  19.     if x + 1 < n and A[x + 1][y] == ".":
  20.         queue.append((x + 1, y, str(int(sub)+1)))
  21.     if y > 0 and A[x][y - 1] == ".":
  22.         queue.append((x, y - 1, str(int(sub)+1)))
  23.     if y + 1 < m and A[x][y + 1] == ".":
  24.         queue.append((x, y + 1, str(int(sub)+1)))
  25.  
  26. with open("output.txt", mode="w") as f_write:
  27.     if A[x_finish][y_finish] == ".":
  28.         print(-1)
  29.     else:
  30.         print(A[x_finish][y_finish])
RAW Paste Data