zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    【洛谷P5514】永夜的报应【模拟】
    当你闲得无聊去编 C++「贪吃蛇」小游戏
    【牛客练习赛50】C
    【JZOJ3410】Tree【最小生成树】
    @Zookeeper可视化工具。 ZK 安装 node-zk-browser。2015.10.22亲测可用
    Zookeeper可视化工具。 ZK 安装 node-zk-browser。2015.10.22亲测可用
    读 Paxos 到 ZooKeeper ¥ 50大洋
    ZooKeeper 分布式锁
    UML 绘图关系
    Astah 使用 流程图、类图、时序图
  • 原文地址:https://www.cnblogs.com/thmyl/p/12250177.html
Copyright © 2011-2022 走看看