zoukankan      html  css  js  c++  java
  • Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力

    D. Vanya and Treasure

    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
    output
    11

    题意:给你一个n*m的方格,你需要把权值为p的那个门打开,前提是你必须打开过权值为p-1的门。然后问你打开权值为p的门,你最少走的距离是多少,一开始你在(1,1)注意:一个点的门即使没有被打开,也是可以走的,只是门没开而已。
    我们对于这个题进行优雅的暴力.
    假设我们现在走的点数为i,那么点数为i的格子的个数如果小于sqrt(n*m)我们直接暴力更新
    如果大于
    sqrt(n*m)我们就跑一遍bfs,这样做均摊了复杂度n^3
    代码如下:
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 const int maxn = 330;
     5 int a[maxn][maxn];
     6 int dp[maxn][maxn];
     7 int inq[maxn][maxn];
     8 int dis[maxn][maxn];
     9 int dx[4]={1,-1,0,0};
    10 int dy[4]={0,0,1,-1};
    11 vector < pair<int,int> > vec[maxn*maxn];
    12 int n,m,p;
    13 int main()
    14 {
    15     //freopen("de.txt","r",stdin);
    16     while (~scanf("%d%d%d",&n,&m,&p)){
    17         for (int i=0;i<maxn*maxn;++i)
    18         vec[i].clear();
    19         memset(inq,0,sizeof inq);
    20         memset(dp,127,sizeof dp);
    21         for (int i=1;i<=n;++i){
    22             for (int j=1;j<=m;++j){
    23                 scanf("%d",&a[i][j]);
    24                 if (a[i][j]==1) dp[i][j] = i+j-2;
    25                 vec[a[i][j]].push_back(make_pair(i,j));
    26             }
    27         }
    28         int level = (int) sqrt(n*m);
    29         int idx = 1;//idx这样处理可以避免memset浪费时间
    30         queue <pair<int,int> > q;
    31         for (int i=1;i<p;++i){
    32             int sz = vec[i].size();
    33             if (sz<=level){
    34                 for (auto u:vec[i]){
    35                     for (auto v:vec[i+1]){
    36                         dp[v.first][v.second] = min(dp[v.first][v.second],dp[u.first][u.second]+abs(u.first-v.first)+abs(u.second-v.second));
    37                     }
    38                 }
    39             }
    40             else{
    41                 idx++;
    42                 for (auto v:vec[i])
    43                     q.push(v);
    44                 memset(dis,127,sizeof dis);
    45                 for (auto v:vec[i])
    46                     dis[v.first][v.second] = dp[v.first][v.second];
    47                 while (!q.empty()){
    48                     pair<int,int > now = q.front();
    49                     q.pop();
    50                     inq[now.first][now.second]=0;
    51                     for (int k=0;k<4;++k){
    52                         pair<int,int> nxt = now;
    53                         nxt.first+=dx[k];
    54                         nxt.second+=dy[k];
    55                         if (nxt.first<1||nxt.first>n) continue;
    56                         if (nxt.second<1||nxt.second>m) continue;
    57                         if (dis[nxt.first][nxt.second]>dis[now.first][now.second]+1){
    58                             dis[nxt.first][nxt.second]=dis[now.first][now.second]+1;
    59                             if (inq[nxt.first][nxt.second]<idx){
    60                                 inq[nxt.first][nxt.second]=idx;
    61                                 q.push(nxt);
    62                             }
    63                         }
    64                     }
    65                 }
    66                 for (auto v:vec[i+1])
    67                     dp[v.first][v.second]=dis[v.first][v.second];
    68             }
    69         }
    70         printf("%d
    ",dp[vec[p][0].first][vec[p][0].second]);
    71     }
    72     return 0;
    73 }
     
    
    
  • 相关阅读:
    Python 读取二进制、HTML 、XML 格式存储的 Excel 文件
    ios自动将长数字(7位)转成电话号码
    前端常见手撕源码
    在微信里及QQ浏览器里ios短信回填vue方法取不到值,去除黄色背景
    js添加css到head中
    WEB 基础认证(BasicAuth)
    Word如何插入PDF格式矢量图片
    【解决】MATLAB中报错:无法将双精度值 0.0401495 转换为句柄
    【解决】Word中插入图片后变模糊
    使用SuperSocket开发联网斗地主(三):抢地主
  • 原文地址:https://www.cnblogs.com/agenthtb/p/7608748.html
Copyright © 2011-2022 走看看