Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n, m = map(int, input().split())
- matrix = []
- while n:
- matrix.append(list(map(int, input().split())))
- n -= 1
- res = []
- q = []
- for i in range(len(matrix)):
- row = []
- for j in range(m):
- if matrix[i][j] == 1:
- row.append(0)
- q.append((i, j))
- else:
- row.append(-1)
- res.append(row)
- while q:
- x, y = q.pop(0)
- for xp, yp in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
- if 0 <= xp < len(matrix) and 0 <= yp < m and res[xp][yp] == -1:
- q.append((xp, yp))
- res[xp][yp] = res[x][y] + 1
- for row in res:
- print(' '.join([str(elem) for elem in row]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement