codeforces 677 D. Vanya and Treasure

原创 2016年06月02日 09:54:55

原创地址: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 nm 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


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces 677D Vanya and Treasure(DP+分治) category:

题意: 一个图上有一些宝箱,每一个级别的箱子里有搞一个级别箱子的钥匙,而最高级别的里面有宝藏,每次从(1,1)出发,问你拿到宝藏所要走的最短距离是多少。 解法: 这题如果直接来暴力更新的话,复...
  • CQUWEL
  • CQUWEL
  • 2017年01月18日 16:17
  • 251

Codeforces 677E Vanya and Balloons【断点前缀和+暴力维护+数学技巧】

E. Vanya and Balloons time limit per test 3 seconds memory limit per test 512 megabytes i...

Codeforces 677D Vanya and Treasure【dp+极限思维剪枝】

D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes ...

codeforces_677D. Vanya and Treasure(BFS+DP)

D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabyte...

codeforce 677D. Vanya and Treasure bfs

D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes ...
  • FTQOOO
  • FTQOOO
  • 2016年06月11日 21:51
  • 209

codeforces round 355 div2 D Vanya and Treasure Dp 二维线段树

#pragma warning(disable:4786) #pragma comment(linker, "/STACK:102400000,102400000") #include #includ...
  • jijijix
  • jijijix
  • 2016年12月16日 22:54
  • 92

Codeforces Round #355 (Div. 2) D. Vanya and Treasure

Vanya is in the palace that can be represented as a grid n × m. Each room contains a single chest, ...

Codeforces 677C Vanya and Label【思维】

C. Vanya and Label time limit per test 1 second memory limit per test 256 megabytes input...

codeforces 677c Vanya and Label

 While walking down the street Vanya saw a label "Hide&Seek". Because he is a programmer, he us...

codeforces 677 B Vanya and Food Processor (模拟)

codeforces #355 B(Div 2)http://http://codeforces.com/contest/677/problem/B B. Vanya and Food Proces...
  • wchhlbt
  • wchhlbt
  • 2016年06月02日 09:27
  • 222
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:codeforces 677 D. Vanya and Treasure
举报原因:
原因补充:

(最多只允许输入30个字)