Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with open("input.txt", mode="r") as f_read:
- n, m = map(int, f_read.readline().strip().split())
- x_start, y_start = map(int, f_read.readline().strip().split())
- x_finish, y_finish = map(int, f_read.readline().strip().split())
- A = [list(i.strip()) for i in f_read.readlines()]
- x_finish -= 1
- y_finish -= 1
- queue = [(x_start - 1, y_start - 1, "0")]
- while len(queue) > 0:
- x, y, sub = queue.pop(0)
- A[x][y] = sub
- if (x, y) == (x_finish, y_finish):
- break
- if x > 0 and A[x - 1][y] == ".":
- queue.append((x - 1, y, str(int(sub)+1)))
- if x + 1 < n and A[x + 1][y] == ".":
- queue.append((x + 1, y, str(int(sub)+1)))
- if y > 0 and A[x][y - 1] == ".":
- queue.append((x, y - 1, str(int(sub)+1)))
- if y + 1 < m and A[x][y + 1] == ".":
- queue.append((x, y + 1, str(int(sub)+1)))
- with open("output.txt", mode="w") as f_write:
- if A[x_finish][y_finish] == ".":
- print(-1)
- else:
- print(A[x_finish][y_finish])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement