Sunsets
Time limit1sMemory limit128 MB
For each cell of an n x n grid, output the tallest height among all cells within Manhattan distance k.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum, Matrix
- Solved
- No attempts yet
Problem
Residents of a small city love watching sunsets from the rooftops of their houses. When a sunset is especially spectacular, some of them climb onto the roofs of nearby buildings to get a better view.
The city's buildings sit on an grid, and the distance between two buildings is measured with the Manhattan metric.
John is about to buy a new flat. He loves sunsets and is willing to walk to another building every evening for a better view, but he will not walk more than units away from his own flat.
For each building , determine the height of the tallest building John could walk to (every building within Manhattan distance , including building itself) if he bought his flat in building . Output the resulting map of these heights.
As a reminder, the Manhattan distance between two points and is .
Input
The first line contains two integers and (, ) separated by a single space. Each of the next lines contains non-negative integers, each at most , separated by single spaces; these are the heights of the buildings, given row by row.
Output
Print lines, each containing non-negative integers separated by single spaces. The integer in row , column is the height of the tallest building within Manhattan distance of building .