CF #355div2 D 宝藏与钥匙 dp 二维数组智商题

D. Vanya and Treasure
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 aij. 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 (r1, c1) (the cell in the row r1 and column c1) and (r2, c2) is equal to |r1 - r2| + |c1 - c2|.

Input

The first line of the input contains three integers nm 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 aij (1 ≤ aij ≤ 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 arc = 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.

Examples
input
3 4 3
2 1 1 1
1 1 1 1
2 1 1 3
output
5
input
3 3 9
1 3 5
8 9 7
4 6 2
output
22
input
3 4 12
1 2 3 4
8 7 6 5
9 10 11 12
outpu11
题意: 给你n*m的个格子,每个格子中都有一个权值(1-p),问你从最左上角的位置开始走,走到
最终权值为p的格子中(只有一个),只要曾经经过权值为i的格子就能打开权值为i+1的格子中的宝藏,现在要打开权值为p的格子中的宝藏问需要经过的最少步数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int  inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=5+100000;
const int mod=1e9+7;

int dis[305][305],vis[305][305],ans[305][305];
struct node{
   int x,y;
}ne[maxn];
vector<node> G[maxn];

ll gdis(node u,node v)
{
    return abs(u.x-v.x)+abs(u.y-v.y);
}

int main()
{
    int n,m,p,v,ex,ey;
    while(~scanf("%d %d %d",&n,&m,&p))
    {
        int cnt=0,k1,k2;
        MM(vis,-1);MM(dis,inf);MM(ans,inf);
        for(int i=1;i<=p;i++) G[i].clear();
        for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
          {
              scanf("%d",&v);
              G[v].push_back((node){i,j});
              if(v==p) {ex=i;ey=j;}
          }
        for(int i=1;i<=m;i++)
          {
             dis[1][i]=i-1;
             vis[1][i]=0;
          }
        for(int u=1;u<=p;u++)
        {
            for(int k=0;k<G[u].size();k++)
            {
                 int r=G[u][k].x,l=G[u][k].y;
                 for(int i=1;i<=n;i++)
                    if(vis[i][l]==u-1)
                     ans[r][l]=min(ans[r][l],dis[i][l]+abs(i-r));
            }
            for(int k=0;k<G[u].size();k++)
            {
                int r=G[u][k].x,l=G[u][k].y;
                for(int j=1;j<=m;j++)
                    if(vis[r][j]!=u)
                    {
                       vis[r][j]=u;
                       dis[r][j]=ans[r][l]+abs(l-j);
                    }
                    else
                      dis[r][j]=min(dis[r][j],ans[r][l]+abs(l-j));
            }
        }
        printf("%d
",ans[ex][ey]);
    }
    return 0;
}

  分析:设置dis和vis以及ans三个数组,假设[i][j]格子中的权值为v,ans[i][j]代表,从最左上角格子开始走,且经过了1-v的格子后到达该格子的最少步数(最终答案就是ans[ex][ey])dis[i][j]代表

在经过了1-vis[i][j]的权值后,到达该格子的最少步数,vis[i][j]代表该格子所在的行所更新的最大的权值

原文地址:https://www.cnblogs.com/smilesundream/p/5559129.html