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
outpu11
题意: 给你n*m的个格子,每个格子中都有一个权值(1-p),问你从最左上角的位置开始走,走到
最终权值为p的格子中(只有一个),只要曾经经过权值为i的格子就能打开权值为i+1的格子中的宝藏,现在要打开权值为p的格子中的宝藏问需要经过的最少步数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=5+100000;
const int mod=1e9+7; int dis[305][305],vis[305][305],ans[305][305];
struct node{
int x,y;
}ne[maxn];
vector<node> G[maxn]; ll gdis(node u,node v)
{
return abs(u.x-v.x)+abs(u.y-v.y);
} int main()
{
int n,m,p,v,ex,ey;
while(~scanf("%d %d %d",&n,&m,&p))
{
int cnt=0,k1,k2;
MM(vis,-1);MM(dis,inf);MM(ans,inf);
for(int i=1;i<=p;i++) G[i].clear();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&v);
G[v].push_back((node){i,j});
if(v==p) {ex=i;ey=j;}
}
for(int i=1;i<=m;i++)
{
dis[1][i]=i-1;
vis[1][i]=0;
}
for(int u=1;u<=p;u++)
{
for(int k=0;k<G[u].size();k++)
{
int r=G[u][k].x,l=G[u][k].y;
for(int i=1;i<=n;i++)
if(vis[i][l]==u-1)
ans[r][l]=min(ans[r][l],dis[i][l]+abs(i-r));
}
for(int k=0;k<G[u].size();k++)
{
int r=G[u][k].x,l=G[u][k].y;
for(int j=1;j<=m;j++)
if(vis[r][j]!=u)
{
vis[r][j]=u;
dis[r][j]=ans[r][l]+abs(l-j);
}
else
dis[r][j]=min(dis[r][j],ans[r][l]+abs(l-j));
}
}
printf("%d\n",ans[ex][ey]);
}
return 0;
}

  分析:设置dis和vis以及ans三个数组,假设[i][j]格子中的权值为v,ans[i][j]代表,从最左上角格子开始走,且经过了1-v的格子后到达该格子的最少步数(最终答案就是ans[ex][ey])dis[i][j]代表

在经过了1-vis[i][j]的权值后,到达该格子的最少步数,vis[i][j]代表该格子所在的行所更新的最大的权值

CF #355div2 D 宝藏与钥匙 dp 二维数组智商题的更多相关文章

  1. 剑指 offer set 1 二维数组中查找

    总结 1. 二维数组搜索题遇到两个了, 一个是 Leetcode 上 search in 2D matrix. 那道题比较简单, 因为下一行的所有元素大于上一行的. 这道题对二维矩阵的要求比较松, 起 ...

  2. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  3. dp --- 二维dp + 最大上升子序列

    <传送门> 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 74477   Accepted: 27574 ...

  4. 问题 A: 【动态规划】采药_二维数组_一维数组

    问题 A: [动态规划]采药 时间限制: 1 Sec  内存限制: 64 MB提交: 35  解决: 15[提交][状态][讨论版] 题目描述 山洞里有一些不同的草药,采每一株都需要一些时间,每一株也 ...

  5. C++二维数组动态内存分配

    对于二维数组和二维指针的内存的分配 这里首选说一下一维指针和一维数组的内存分配情况. 一维: 数组:形如int  a[5];这里定义了一个一维数组a,并且数组的元素个数是5,这里的a是这五个元素的整体 ...

  6. (二维数组 亿进制 或 滚动数组) Hat's Fibonacci hdu1250

    Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. C语言复习---二维数组和二级指针的关系:没关系,别瞎想(重点)

    前提:一维数组和一维指针为什么可以替换使用? ] = { , , }; int *p = a; ; i < ; i++) printf("%d ", *(p + i)); 上 ...

  8. Poj1050_To the Max(二维数组最大字段和)

    一.Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is an ...

  9. C++中vecotr表示二维数组并自己实现一个Grid类

    1 C++中使用vector来表示二维数组 声明一个二维数组: vector<vector<int>> dp(row, vector<int>(col)); 将变量 ...

随机推荐

  1. 【转】.NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现

    作者:Zhang_Xiang 原文地址:.NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现 先决条件 关于 Ocelot 针对使用 .NET 开发 ...

  2. Magento2入门之修改logo

    本文用于学习记录用 1.主题创建是在路径 /app/design/frontend/公司名/主题名称/ 我自己创建的路径为 app/design/frontend/Bman/castle,以下操作都在 ...

  3. js中的alert弹出框文字乱码解决方案

    使用如下代码即可: echo '<html>'; echo '<head><meta http-equiv="Content-Type" conten ...

  4. jquery的ajax方法使用application/json出现400错误码的解决方案

    400说明是客户端错误,将contentType默认的application/x-www-form-urlencoded改成application/json就出现错误,说明传输的数据不是JSON. 解 ...

  5. Spring Cloud netflix 概览和架构设计

    pring Cloud是基于Spring Boot的一整套实现微服务的框架.他提供了微服务开发所需的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集群状态管理等 ...

  6. 转 eclipse 快捷键

    1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如applic*.xm ...

  7. CentOS7搭建FastDFS V5.11分布式文件系统(三)

    1.测试 前面两篇博文已对FastDFS的安装和配置,做了比较详细的讲解.FastDFS的基础模块都搭好了,现在开始测试下载. 1.1 配置客户端 同样的,需要修改客户端的配置文件: /etc/fdf ...

  8. 全局捕获异常(适用于SpringMvc,SpringBoot项目)

    @ControllerAdvice 是controller的一个辅助类,最常用的就是作为全局异常处理的切面类.约定了几种可行的返回值,可以返回String字符串,也可以返回ModelAndView,也 ...

  9. ShuffleNet系列学习笔记

    ShuffleNet是旷世提出的高效轻量化网络,是一款很值得一提的轻量化网络,其相关论文也是很有价值的. ShuffleNet V1 该网络提出于2017年,论文为<ShuffleNet: An ...

  10. 神经网络优化算法:梯度下降法、Momentum、RMSprop和Adam

    最近回顾神经网络的知识,简单做一些整理,归档一下神经网络优化算法的知识.关于神经网络的优化,吴恩达的深度学习课程讲解得非常通俗易懂,有需要的可以去学习一下,本人只是对课程知识点做一个总结.吴恩达的深度 ...