#P15674. [ICPC 2024 Jakarta R] X Aura
[ICPC 2024 Jakarta R] X Aura
题目描述
Mount ICPC can be represented as a grid of rows (numbered from to ) and columns (numbered from to ). The cell located at row and column is denoted as and has a height of . Two cells are adjacent to each other if they share a side. Formally, is adjacent to , , , and , if any exists.
You can move only between adjacent cells, and each move comes with a penalty. With an aura of an odd positive integer , moving from a cell with height to a cell with height gives you a penalty of . Note that the penalty can be negative.
You want to answer independent scenarios. In each scenario, you start at the starting cell and you want to go to the destination cell with minimum total penalty. In some scenarios, the total penalty might become arbitrarily small; such a scenario is called invalid. Find the minimum total penalty to move from the starting cell to the destination cell, or determine if the scenario is invalid.
输入格式
The first line consists of three integers ( is an odd integer).
Each of the next lines consists of a string of length . Each character in is a number from 0 to 9. The -th character of represents the height of cell , or .
The next line consists of an integer ( .
Each of the next lines consists of four integers ( ).
输出格式
For each scenario, output the following in a single line. If the scenario is invalid, output INVALID. Otherwise, output a single integer representing the minimum total penalty to move from the starting cell to the destination cell.
3 4 1
3359
4294
3681
5
1 1 3 4
3 3 2 1
2 2 1 4
1 3 3 2
1 1 1 1
2
4
-7
-1
0
2 4 5
1908
2023
2
1 1 2 4
1 1 1 1
INVALID
INVALID
提示
Explanation for the sample input/output #1
For the first scenario, one of the solutions is to move as follows: $ (1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4) $ . The total penalty of this solution is $ (3 - 4)^1 + (4 - 3)^1 + (3 - 6)^1 + (6 - 8)^1 + (8 - 1)^1 = 2 $ .
Explanation for the sample input/output #2
For the first scenario, the cycle $ (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 1) $ has a penalty of $ (1 - 2)^5 + (2 - 0)^5 + (0 - 9)^5 + (9 - 1)^5 = -26250 $ . You can keep repeating this cycle to make your total penalty arbitrarily small. Similarly, for the second scenario, you can move to first, then repeat the same cycle.