1. 程式人生 > >codeforces 677D. Vanya and Treasure(dp+bfs,黑科技)

codeforces 677D. Vanya and Treasure(dp+bfs,黑科技)

題目連結

D. Vanya and Treasure
time limit per test1.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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

題意:給出一個 nm 的矩陣,裡面有一個 1p 的數字,代表寶藏的等級,每次獲得了第 x 等級的寶藏你可以獲得第 x+1 的寶藏的鑰匙,初始時所有等級為1的寶藏都是開啟的,對於每個格子,即使沒有開啟該處寶藏,也能經過該格子,問你最少多少步能夠開啟任意一個等級為 p 的寶藏。

題解:記 d[i][j] 為開啟 (i,j) 處寶藏的最少步數,那麼我們很容易想到一個 dp 方程,得到每個等級 xd 陣列後,我們可以更新等級為 x+1d 陣列,不過這麼做是 O(n2×m2) 的,引用一個黑科技,記等級為 i 的格子有 cnt[i] 個,若 cnt[x]×cnt[x+1]<n×m,那麼像上面一樣暴力更新,否則,將所有等級為 x 的格子作為起點,對全圖做一次多源最短路,這樣均攤下來複雜度是 O(nmnm) 的。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=1e9;
const ll mod=1000000007;
const int maxn=300+10;
int a[maxn][maxn];
int dis[maxn][maxn],d[maxn][maxn];
vector<PII> V[maxn*maxn];
int ab(int x)
{
    return x>0? x:-x;
}
int cnt=0;
int inq[maxn][maxn];
PII q[maxn*maxn];
int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int main()
{
    int n,m,p;
    scanf("%d%d%d",&n,&m,&p);
    rep(i,1,n+1)
    {
        rep(j,1,m+1)
        {
            int v;
            scanf("%d",&v);
            V[v].pb(make_pair(i,j));
            if(v==1) d[i][j]=i-1+j-1;
            else d[i][j]=inf;
        }
    }
    rep(i,1,p)
    {
        int t=V[i].size(),t1=V[i+1].size();
        if(t*t1<n*m)
        {
            rep(k,0,t) rep(j,0,t1)
            {
                int x1=V[i][k].fi,y1=V[i][k].se,x2=V[i+1][j].fi,y2=V[i+1][j].se;
                d[x2][y2]=min(d[x2][y2],d[x1][y1]+ab(x2-x1)+ab(y2-y1));
            }
        }
        else
        {
            cnt++;
            rep(i,1,n+1) rep(j,1,m+1) dis[i][j]=inf;
            int front=0,rear=0;
            rep(k,0,t)
            {
                dis[V[i][k].fi][V[i][k].se]=d[V[i][k].fi][V[i][k].se];
                inq[V[i][k].fi][V[i][k].se]=cnt;
                q[rear++]=V[i][k];
            }
            while(front!=rear)
            {
                PII u=q[front++];
                inq[u.fi][u.se]=0;
                if(front>=maxn*maxn) front=0;
                rep(k,0,4)
                {
                    int x=u.fi+dx[k],y=u.se+dy[k];
                    if(x<1||x>n||y<1||y>m) continue;
                    if(dis[x][y]>dis[u.fi][u.se]+1)
                    {
                        dis[x][y]=dis[u.fi][u.se]+1;
                        if(inq[x][y]==cnt) continue;
                        inq[x][y]=cnt;
                        q[rear++]=PII(x,y);
                        if(rear>=maxn*maxn) rear=0;
                    }
                }
            }
            rep(k,0,t1) d[V[i+1][k].fi][V[i+1][k].se]=dis[V[i+1][k].fi][V[i+1][k].se];
        }
    }
    int ans=inf;
    rep(i,0,V[p].size()) ans=min(ans,d[V[p][i].fi][V[p][i].se]);
    printf("%d\n",ans);
    return 0;
}