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

185 篇文章 0 订阅
116 篇文章 0 订阅

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 thei-th row and j-th columns contains the chest of typeaij. Each chest of typex ≤ p - 1 contains a key that can open any chest of typex + 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 rowr1 and columnc1) 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 ofr and c, such thatarc = x). Also, it's guaranteed that there is exactly one chest of typep.

Output

Print one integer — the minimum possible total distance Vanya has to walk in order to get the treasure from the chest of typep.

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


题意:给你一个由1到p数字组成的n*m矩阵,每个数字的门中有开下一个数字的门的钥匙,问你要开p门最少要走多少步。


分析:很容易想到我们可以以数字为阶段做一次DP,但这样如果同一阶段的状态太多可能会T掉,这种情况我们可以暴力跑一次最短路。


#include <queue>
#include <vector>
#include <cstdio>
#include <utility>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f 
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> pii2;
const int F[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int n,m,p,x,f[305][305],dis[305][305],size[90005];
bool jud[305][305];
vector <pii> g[90005];
int main()
{
	memset(f,INF,sizeof(f));
	scanf("%d%d%d",&n,&m,&p);
	for(int i = 1;i <= n;i++)
	 for(int j = 1;j <= m;j++)
	 {
	 	scanf("%d",&x);
	 	g[x].push_back(make_pair(i,j));
		if(x == 1) f[i][j] = i+j-2;
		size[x]++;
	 }
	for(int i = 2;i <= p;i++)
	{
		if(size[i]*size[i-1] <= m*n)
		 for(pii u : g[i])
		  for(pii v : g[i-1])
		  {
		  	 int x = u.first,y = u.second;
		  	 int a = v.first,b = v.second;
		 	 f[x][y] = min(f[x][y],f[a][b]+abs(x-a)+abs(y-b));
		  }
		else 
		{
		    deque <pii2> q;
			memset(jud,0,sizeof(jud));
			memset(dis,INF,sizeof(dis));
			for(pii v : g[i-1]) 
			{
				int a = v.first,b = v.second;
				q.push_back(make_pair(f[a][b],make_pair(a,b)));
				jud[a][b] = true;
				dis[a][b] = f[a][b];
			} 
			while(!q.empty())
			{
				pii2 now = q.front();
				int a = now.second.first,b = now.second.second;
				for(int k = 0;k < 4;k++)
				{
					int x = a+F[k][0],y = b+F[k][1];
					if(x <= n && x && y <= m && y && dis[x][y] > dis[a][b]+1)
					{
						dis[x][y] = dis[a][b] + 1;
						if(!jud[x][y])
						{
							jud[x][y] = true;
							q.push_back(make_pair(dis[x][y],make_pair(x,y)));	
						}
					} 
				}
				jud[a][b] = false;
				q.pop_front();
			}
			for(pii u : g[i])
			{
				int x = u.first,y = u.second;
				f[x][y] = dis[x][y];
			}
		}
	}
	cout<<f[g[p][0].first][g[p][0].second]<<endl;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值