logo AlgoBeat OnlineJudge
登录 注册

#215729. [ICPC 2024 Jakarta R] X Aura

内存限制:1024 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

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.

样例

样例输入 1

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

样例输出 1

2
4
-7
-1
0

样例输入 2

2 4 5
1908
2023
2
1 1 2 4
1 1 1 1

样例输出 2

INVALID
INVALID

数据范围与提示

Explanation for the sample input/output #1

For the first scenario, one of the solutions is to move as follows: . The total penalty of this solution is .

Explanation for the sample input/output #2

For the first scenario, the cycle has a penalty of . 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.