1. 程式人生 > >Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力

Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力

復雜度 font level -1 exactly tput %d test air

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 n

, m 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\n",dp[vec[p][0].first][vec[p][0].second]);
71     }
72     return 0;
73 }

 



Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力