X Aura
Time limit1sMemory limit2048 MB
Given a grid of digit heights and an odd exponent X, answer queries for the minimum total penalty of moving between two cells, where each step costs (h1-h2)^X, or report INVALID if the cost can go unbounded below.
- Level
Hard9 of 10
- Topics
- Graph, Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
Mount ICPC can be represented as a grid of rows (numbered from to ) and columns (numbered from to ). The cell at row and column is denoted and has height . Two cells are adjacent if they share a side. So is adjacent to , , , and , whenever those cells exist.
You can move only between adjacent cells, and each move has a penalty. With an aura , which is an odd positive integer, moving from a cell with height to a cell with height gives a penalty of . The penalty can be negative.
There are independent scenarios. In each one, you start at cell and want to reach cell with the minimum total penalty. In some scenarios the total penalty can become arbitrarily small. Such a scenario is called invalid. For each scenario, find the minimum total penalty, or determine that it is invalid.
Input
The first line contains three integers , , and (; ; is odd).
Each of the next lines contains a string of length . Each character is a digit from 0 to 9. The -th character of is the height of cell .
The next line contains an integer ().
Each of the next lines contains four integers (; ).
Output
For each scenario, print one line. If the scenario is invalid, print INVALID. Otherwise, print the minimum total penalty from the starting cell to the destination cell as an integer.