Nearest number - 2
InterviewTime limit1sMemory limit128 MB
Replace each zero with the nearest nonzero value using Manhattan distance, but leave zeros that are tied between two or more nearest values unchanged.
- Level
Medium5 of 10
- Topics
- BFS, Graph, Matrix, Implementation
- Solved
- No attempts yet
Problem
You are given an matrix of non-negative integers.
The distance between two elements and is defined as .
Replace every zero element of the matrix with the value of the nearest non-zero element. If two or more non-zero elements are tied for the minimum distance, leave that zero unchanged.
Input
The first line contains the integer . It is followed by integers giving the matrix in row-major order.
Output
Print the resulting matrix. Output each row on its own line, with the integers of a row separated by single spaces.
Constraints
- ,