Alien Journey
InterviewTime limit5sMemory limit2048 MB
Find the least lift height for an l by l square ship to move from the top-left to the bottom-right corner of a height grid.
- Level
Medium6 of 10
- Topics
- Shortest path, Sliding window, Matrix
- Solved
- No attempts yet
Problem
A tiny alien asks you for help getting his square spaceship across town to an interdimensional gateway. He has very little fuel, so he needs the minimum amount of power to cross the city. Each unit of fuel lifts the spaceship one unit in height.
The spaceship must fly over every cell beneath it while it moves. It moves north, south, east, or west without using fuel, and the whole ship must stay inside the map at all times.
The ship starts on the ground, and the area it starts on is at height 0. The top-left corner cell of the ship overlays the top-left corner cell of the map, and all four edges of the ship are aligned with the grid. To be above the ground, the bottom of the ship must be strictly higher than the height of every ground cell beneath it. The ship can only be lifted an integer number of units.
The journey ends when the bottom-right corner cell of the ship is floating above the bottom-right corner cell of the map.
The map is a grid of rows and columns, and each cell has a height relative to sea level. The spaceship is a square with edge length , where .
Find the minimum amount of fuel the alien needs.
Input
The first line contains three integers , , and with , , and .
Each of the next lines contains integers in the range . The -th integer on the -th line is the height of the cell at row and column .
Output
Print the minimum amount of power units the alien needs to traverse the city with the spaceship.