Codeforces Round 355 (Div. 2)
D
Vanya and Treasure
Codeforces
2300
原题
← CE →
翻译中…

Vanya is in the palace that can be represented as a grid $*n* × *m*$. Each room contains a single chest, an the room located in the $*i*$-th row and $*j*$-th columns contains the chest of type $*a*_{*ij*}$. Each chest of type $*x* ≤ *p* - 1$ contains a key that can open any chest of type $*x* + 1$, and all chests of type $1$ are not locked. There is exactly one chest of type $*p*$ and it contains a treasure.

翻译中…

Vanya starts in cell $(1, 1)$ (top left corner). What is the minimum total distance Vanya has to walk in order to get the treasure? Consider the distance between cell $(*r*_{1}, *c*_{1})$ (the cell in the row $*r*_{1}$ and column $*c*_{1}$) and $(*r*_{2}, *c*_{2})$ is equal to $|*r*_{1} - *r*_{2}| + |*c*_{1} - *c*_{2}|$.

翻译中…

Input

翻译中…

The first line of the input contains three integers $*n*$, $*m*$ and $*p*$ ($1 ≤ *n*, *m* ≤ 300, 1 ≤ *p* ≤ *n*·*m*$) — the number of rows and columns in the table representing the palace and the number of different types of the chests, respectively.

翻译中…

Each of the following $*n*$ lines contains $*m*$ integers $*a*_{*ij*}$ ($1 ≤ *a*_{*ij*} ≤ *p*$) — the types of the chests in corresponding rooms. It's guaranteed that for each $*x*$ from $1$ to $*p*$ there is at least one chest of this type (that is, there exists a pair of $*r*$ and $*c*$, such that $*a*_{*rc*} = *x*$). Also, it's guaranteed that there is exactly one chest of type $*p*$.

翻译中…

Output

翻译中…

Print one integer — the minimum possible total distance Vanya has to walk in order to get the treasure from the chest of type $*p*$.

翻译中…

样例

Input #1
3 4 3
2 1 1 1
1 1 1 1
2 1 1 3
Output #1
5
InputCopy OutputCopy
Input #2
3 3 9
1 3 5
8 9 7
4 6 2
Output #2
22
InputCopy OutputCopy
Input #3
3 4 12
1 2 3 4
8 7 6 5
9 10 11 12
Output #3
11
InputCopy OutputCopy
题目信息
题号
677D
时间限制
1.5 s
内存限制
256 MB
标签
数据结构
动态规划
图论
最短路

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。