Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- head = 0
- q = []
- def isempty():
- return head == len(q)
- def push(val):
- q.append(val)
- def pop():
- global head
- head += 1
- def front():
- return q[head]
- n, m = map(int, input().split())
- s = list(map(int, input().split()))
- t = list(map(int, input().split()))
- g = []
- for i in range(n):
- g.append(str(input()))
- d = []
- for i in range(n):
- d.append([])
- for j in range(m):
- d[i].append(-1)
- dx = [0, -1, 0, 1]
- dy = [-1, 0, 1, 0]
- d[s[0]][s[1]] = 0
- push(s)
- def check(x, y):
- return 0 <= x and x < n and 0 <= y and y < m and g[x][y] != 'X'
- if (not check(s[0], s[1])):
- print('INF')
- exit()
- if (not check(t[0], t[1])):
- print('INF')
- exit()
- ans = []
- while (not isempty()):
- u = front()
- x = u[0]
- y = u[1]
- pop()
- for i in range(4):
- nx = x + dx[i]
- ny = y + dy[i]
- if (check(nx, ny)):
- if (d[nx][ny] == -1):
- d[nx][ny] = d[x][y] + 1
- push((nx, ny))
- if (d[t[0]][t[1]] == -1):
- print('INF')
- exit()
- print(d[t[0]][t[1]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement