您的位置:首页 > 其它

codeforces 677 D. Vanya and Treasure

2016-06-02 09:54 369 查看
原创地址:http://blog.csdn.net/stl112514/article/details/51563993

做法:每次可以把已经知道ans的点都拿出来,若它的位置是(r,c),则更新所有的dis[r][k]=min(dis[r][k],ans[r][c]+abs(c-k)),1<=k<=m。也就是用横线维护dis,再用纵线维护ans,ans[r][c]=min(ans[r][c],dis[k][c]+abs(c-r)),1<=k<=n,当然了,还需要维护一个vis[][]标记出所有位置当前被携带了哪把钥匙访问过,只有满足(k,c)的钥匙能打开(r,c)的箱子时,才去更新。其实纵横线的意义可以随意设定了,想法还是很妙的。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef pair<int,int> pp;
vector<pp>bx[300*300+10];
int vis[310][310];
int ans[310][310];
int dis[310][310];
int main()
{
int n,m,p;
cin>>n>>m>>p;
memset(ans,63,sizeof(ans));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
int t;
cin>>t;
bx[t].push_back(pp(i,j));
}
bx[0].push_back(pp(1,1));
for(int i=0;i<=p;i++)
{
int len=bx[i].size();
for(int j=0;j<len;j++)
{
int r=bx[i][j].first,c=bx[i][j].second;
if(r==1&&c==1&&ans[r][c]==0)
ans[r][c]=1000000000;
for(int k=1;k<=n;k++)
if(vis[k][c]==i)
ans[r][c]=min(ans[r][c],dis[k][c]+abs(k-r));
}
for(int j=0;j<len;j++)
{
int r=bx[i][j].first,c=bx[i][j].second;
for(int k=1;k<=m;k++)
if(vis[r][k]!=i+1)
{
vis[r][k]=i+1;
dis[r][k]=ans[r][c]+abs(k-c);
}
else
dis[r][k]=min(dis[r][k],ans[r][c]+abs(k-c));
}
}
cout<<ans[bx[p][0].first][bx[p][0].second];
}


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 58 9 7
4 6 2


output
22


input
3 4 12
1 2 3 4
8 7 6 59 10 11 12


output
11
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: