CF677D.Vanya and Treasure

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Vanya is in the palace that can be represented as a grid n×mn×m . Each room contains a single chest, an the room located in the ii -th row and jj -th columns contains the chest of type aija_{ij} . Each chest of type x<=p1x<=p-1 contains a key that can open any chest of type x+1x+1 , and all chests of type 11 are not locked. There is exactly one chest of type pp and it contains a treasure.

Vanya starts in cell (1,1)(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 (r1,c1)(r_{1},c_{1}) (the cell in the row r1r_{1} and column c1c_{1} ) and (r2,c2)(r_{2},c_{2}) is equal to r1r2+c1c2|r_{1}-r_{2}|+|c_{1}-c_{2}| .

输入格式

The first line of the input contains three integers nn , mm and pp ( 1<=n,m<=300,1<=p<=nm1<=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 nn lines contains mm integers aija_{ij} ( 1<=aij<=p1<=a_{ij}<=p ) — the types of the chests in corresponding rooms. It's guaranteed that for each xx from 11 to pp there is at least one chest of this type (that is, there exists a pair of rr and cc , such that arc=xa_{rc}=x ). Also, it's guaranteed that there is exactly one chest of type pp .

输出格式

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

输入输出样例

  • 输入#1

    3 4 3
    2 1 1 1
    1 1 1 1
    2 1 1 3
    

    输出#1

    5
    
  • 输入#2

    3 3 9
    1 3 5
    8 9 7
    4 6 2
    

    输出#2

    22
    
  • 输入#3

    3 4 12
    1 2 3 4
    8 7 6 5
    9 10 11 12
    

    输出#3

    11
    
首页