• Codeforces 677D


    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
    Copy
    3 4 3
    2 1 1 1
    1 1 1 1
    2 1 1 3
    output
    Copy
    5
    input
    Copy
    3 3 9
    1 3 5
    8 9 7
    4 6 2
    output
    Copy
    22
    input
    Copy
    3 4 12
    1 2 3 4
    8 7 6 5
    9 10 11 12
    output
    Copy
    11

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<cmath> 
    #define maxn 310
    using namespace std;
    struct node{
        int dis,pos;
    }b[3050000],now;
    int a[maxn][maxn];
    int dp[maxn][maxn];
    bool cmp(node a,node b){
        return a.dis<b.dis;
    }
    vector<node>mp[maxn*maxn];
    int main(){
        int n,m,p;
        scanf("%d%d%d",&n,&m,&p);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                dp[i][j]=0x3f3f3f3f;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&a[i][j]);
                if(a[i][j]==1){
                    dp[i][j]=i+j-2;
                    now.dis=dp[i][j];
                }
                now.pos=(i-1)*m+j;
                mp[a[i][j]].push_back(now);
            }
        }
        sort(mp[1].begin(),mp[1].end(),cmp);
        for(int i=2;i<=p;i++){
            int sz1=mp[i].size();
            int sz2=mp[i-1].size();
            for(int j=0;j<sz1;j++){
                int x=mp[i][j].pos/m+1;
                int y=mp[i][j].pos%m;
                if(y==0)y=m,x--;
                for(int k=0;k<sz2&&k<1000;k++){
                    int xx=mp[i-1][k].pos/m+1;
                    int yy=mp[i-1][k].pos%m;
                    if(yy==0)yy=m,xx--;
                    dp[x][y]=min(dp[x][y],dp[xx][yy]+abs(x-xx)+abs(y-yy));
                }
                mp[i][j].dis=dp[x][y];
            }
            sort(mp[i].begin(),mp[i].end(),cmp);
        }
        int ans=0x3f3f3f3f;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                if(a[i][j]==p)
                ans=min(dp[i][j],ans);
            }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    valgrind内存检测
    uosdeepin商店下载的软件deb包位置
    jmeter中获取到token,又因为现在都是JWT的所以需要添加Bearer 和获取到的token进行拼接
    jmeter json提取器
    jmeter命令行启动
    linux 中把文件内容变成空,不删除文件
    94--分布式事务五-Seata AT模式-Spring Cloud微服务案例(添加AT事务)
    94--Docker(概念/镜像操作/数据管理/网络/互联/构建镜像)
    93--分布式事务四-Seata AT模式-Spring Cloud微服务案例(无事务)
    93--分布式事务二-Seata AT 模式
  • 原文地址:https://www.cnblogs.com/thmyl/p/12250177.html
Copyright © 2020-2023  润新知