首页 技术 正文
技术 2022年11月20日
0 收藏 753 点赞 4,152 浏览 2870 个字

D. Vanya and Treasure

题目连接:

http://www.codeforces.com/contest/677/problem/D

Description

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.

The only line of the input contains a single word s (1 ≤ |s| ≤ 100 000), consisting of digits, lowercase and uppercase English letters, characters ‘-‘ and ‘_’.

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.

Sample Input

3 4 3

2 1 1 1

1 1 1 1

2 1 1 3

Sample Output

5

Hint

题意

给你一个n*m的方格,你需要把权值为p的那个门打开,前提是你必须打开过权值为p-1的门……

然后问你打开权值为p的门,你最少走的距离是多少,一开始你在(1,1)

题解:

n*mlog的就扫描线莽一波

n^3的话,就分治一下就好了,如果当前权值的数量小于sqrt(nm)的话,我就暴力更新,否则我就在全图进行一次bfs

这样均摊下来,复杂度是nmsqrt(n)sqrt(m)的

所以直接暴力吧

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 305;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int a[maxn][maxn],dp[maxn][maxn],n,m,p;
int dis[maxn][maxn],inq[maxn][maxn];
vector<pair<int,int> >V[maxn*maxn];
int main()
{
memset(dp,127,sizeof(dp));
scanf("%d%d%d",&n,&m,&p);
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-1+j-1;
V[a[i][j]].push_back(make_pair(i,j));
}
}
int level = sqrt(n*m);
int idx=1;
queue<pair<int,int> >Q;
for(int i=1;i<p;i++)
{
int sz = V[i].size();
if(sz<=level){
for(auto v:V[i+1])
for(auto u:V[i])
dp[v.first][v.second]=min(dp[v.first][v.second],dp[u.first][u.second]+abs(v.first-u.first)+abs(v.second-u.second));
}
else{
idx++;
for(auto v:V[i])
Q.push(v);
memset(dis,127,sizeof(dis));
for(auto v:V[i])
dis[v.first][v.second]=dp[v.first][v.second];
while(!Q.empty())
{
pair<int,int>now=Q.front();
Q.pop();
inq[now.first][now.second]=0;
for(int k=0;k<4;k++)
{
pair<int,int>next=now;
next.first+=dx[k];
next.second+=dy[k];
if(next.first<1||next.first>n)continue;
if(next.second<1||next.second>m)continue;
if(dis[next.first][next.second]>dis[now.first][now.second]+1)
{
dis[next.first][next.second]=dis[now.first][now.second]+1;
if(inq[next.first][next.second]<idx){
inq[next.first][next.second]=idx;
Q.push(next);
}
}
}
}
for(auto v:V[i+1])
dp[v.first][v.second]=dis[v.first][v.second];
}
}
cout<<dp[V[p][0].first][V[p][0].second]<<endl;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844