codeforce 677D. Vanya and Treasure bfs

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 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的图,每个位置有一个1到p的数字(只有一个p,其他数字可以很多),且每个位置有一把锁,每个数字的锁有打开当前数字+1的钥匙。起点为(1,1),数字为1的格子都是解锁的,问走到数字p最少走多少步。一个点到另外一个点的距离为abs(x2-x1)+abs(y2-y1)。


理解:每个数字由前一个数字更新而来,暴力记录每个点的坐标和值,那么当前假设数字为i,它由i-1更新而来,i-1在图中有a个,i有b个,那么更新的复杂度为a*b。数据较小可以,但是本题300*300,如果对半分的话,45000*45000,根据我以前测的cf效率,1s大概能跑15亿到17亿左右(只有i++操作),这个刚好被卡了。


然后看了别人的,当a*b大于n*m的时候,采用bfs更新每个点,然后取需要的点就行,由于多个起点,所以还有些小细节,觉得很奇妙。


CODE

#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pi;
typedef pair<int,pi> pii;
const int N = 300+10;
const int INF = 0x7fffffff/2;

int n,m,p;
int ans[N][N];         ///存答案
int dis[N][N];        ///跑bfs用
bool vis[N][N];      ///bfs标记数组
vector<pi> G[N*N];  ///每个数字对应的坐标
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};

void Init()       ///初始化
{
    for(int i = 0;i < N;i++){
        for(int j = 0;j <N;j++){
            ans[i][j] = INF;
        }
    }
}

int main()
{
    Init();
    scanf("%d%d%d",&n,&m,&p);
    for(int i = 1;i <= n;i++)
    for(int j = 1;j <= m;j++){
        int t;
        scanf("%d",&t);
        G[t].push_back(make_pair(i,j));
        if(t == 1)
            ans[i][j] = i+j-2;
    }
    for(int i = 2;i <= p;i++){
        if(G[i].size()*G[i].size() <= n*m){         ///小于就暴力更新
            for(int j = 0;j < G[i-1].size();j++){
                for(int k = 0;k < G[i].size();k++){
                    int x1 = G[i-1][j].first,y1 = G[i-1][j].second;
                    int x2 = G[i][k].first,  y2 = G[i][k].second;
                    ans[x2][y2] = min(ans[x2][y2],ans[x1][y1]+abs(x2-x1)+abs(y2-y1));
                }
            }
        }
        else{
            for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) dis[i][j] = INF,vis[i][j] = false;
            queue<pii> q;
            for(int j = 0;j < G[i-1].size();j++){
                int x = G[i-1][j].first,y = G[i-1][j].second;
                q.push(make_pair(ans[x][y],make_pair(x,y)));
                dis[x][y] = ans[x][y];
            }
            while(!q.empty()){
                pii u = q.front();
                q.pop();
                int x = u.second.first,y = u.second.second;
                for(int j = 0;j < 4;j++){
                    int tx = x+dir[j][0],ty = y+dir[j][1];
                    if(tx < 1 || ty < 1 || tx > n || ty > m) continue;
                    if(dis[tx][ty] > dis[x][y]+1){
                        dis[tx][ty] = dis[x][y]+1;   ///这个点还在队列里面,所以更新这个点的dis就行了
                        if(!vis[tx][ty]){
                            vis[tx][ty] = true;
                            q.push(make_pair(dis[tx][ty],make_pair(tx,ty)));
                        }
                    }
                }
                vis[x][y] = false;    ///多个起点,这个点可能还会被更新到,所以要取消标记
            }
            for(int j = 0;j < G[i].size();j++){
                int x = G[i][j].first,y = G[i][j].second;
                ans[x][y] = min(ans[x][y],dis[x][y]);
            }
        }
    }
    printf("%d\n",ans[G[p][0].first][G[p][0].second]);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值