Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://habr.com/ru/post/264189/
- # волновой алгоритм
- n, m = map(int, input().split())
- a = []
- for i in range(n):
- a.append([0] * m)
- for i in range(n):
- s = input()
- for j in range(m):
- if s[j] == "1":
- start = [i, j]
- elif s[j] == "0":
- finish = [i, j]
- elif s[j] == "#":
- a[i][j] = -1
- d = 0
- if a[0][1] == 0:
- a[0][1] = d + 1
- if a[1][0] == 0:
- a[1][0] = d + 1
- d = 1
- move = True
- while move:
- for i in range(n):
- for j in range(m):
- if a[i][j] == d:
- if [i, j] == finish:
- move = False
- break
- if i + 1 < n:
- if a[i + 1][j] == 0:
- a[i + 1][j] = d + 1
- if i - 1 >= 0:
- if a[i - 1][j] == 0:
- a[i - 1][j] = d + 1
- if j + 1 < m:
- if a[i][j + 1] == 0:
- a[i][j + 1] = d + 1
- if j - 1 >= 0:
- if a[i][j - 1] == 0:
- a[i][j - 1] = d + 1
- d += 1
- print(start)
- print(finish)
- for x in a:
- print(*x)
Advertisement
Add Comment
Please, Sign In to add comment