CF #355div2 D 宝藏与钥匙 dp 二维数组智商题
1.5 seconds
256 megabytes
standard input
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|.
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.
Print one integer — the minimum possible total distance Vanya has to walk in order to get the treasure from the chest of type p.
3 4 3
2 1 1 1
1 1 1 1
2 1 1 3
5
3 3 9
1 3 5
8 9 7
4 6 2
22
3 4 12
1 2 3 4
8 7 6 5
9 10 11 12
最终权值为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 二维数组智商题的更多相关文章
- 剑指 offer set 1 二维数组中查找
总结 1. 二维数组搜索题遇到两个了, 一个是 Leetcode 上 search in 2D matrix. 那道题比较简单, 因为下一行的所有元素大于上一行的. 这道题对二维矩阵的要求比较松, 起 ...
- 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP
题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...
- dp --- 二维dp + 最大上升子序列
<传送门> 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74477 Accepted: 27574 ...
- 问题 A: 【动态规划】采药_二维数组_一维数组
问题 A: [动态规划]采药 时间限制: 1 Sec 内存限制: 64 MB提交: 35 解决: 15[提交][状态][讨论版] 题目描述 山洞里有一些不同的草药,采每一株都需要一些时间,每一株也 ...
- C++二维数组动态内存分配
对于二维数组和二维指针的内存的分配 这里首选说一下一维指针和一维数组的内存分配情况. 一维: 数组:形如int a[5];这里定义了一个一维数组a,并且数组的元素个数是5,这里的a是这五个元素的整体 ...
- (二维数组 亿进制 或 滚动数组) Hat's Fibonacci hdu1250
Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- C语言复习---二维数组和二级指针的关系:没关系,别瞎想(重点)
前提:一维数组和一维指针为什么可以替换使用? ] = { , , }; int *p = a; ; i < ; i++) printf("%d ", *(p + i)); 上 ...
- Poj1050_To the Max(二维数组最大字段和)
一.Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is an ...
- C++中vecotr表示二维数组并自己实现一个Grid类
1 C++中使用vector来表示二维数组 声明一个二维数组: vector<vector<int>> dp(row, vector<int>(col)); 将变量 ...
随机推荐
- C++序列容器之 vector常见用法总结
一.关于vector 本文默认读者具有一定的c++基础,故大致叙述,但保证代码正确. vector是一个动态的序列容器,相当于一个size可变的数组. 相比于数组,vector会消耗更多的内存以有效的 ...
- 用python操作mysql数据库
数据库的安装和连接 PyMySQL的安装 pip install PyMySQL python连接数据库 import pymysql db = pymysql.connect("数据库ip ...
- python PEP8常用规范(看完你会感谢我的!)
完整的规范移步传送门 pep8规范 官方文档:[https://www.python.org/dev/peps/pep-0008/](https://www.python.org/dev/peps/p ...
- Django重写用户模型报错has no attribute 'USERNAME_FIELD'
目录 Django重写用户模型报错has no attribute 'USERNAME_FIELD' 在重写用户模型时报错:AttributeError: type object 'UserProfi ...
- linux下安装php的lua扩展
1. 进入管理员权限使用yum安装 readline(也可以使用wget下载后./configure 然后 make && make install进行安装) yum install ...
- GoAccess安装
编译安装 yum install geoip-devel openssl-devel libmaxminddb-devel ncurses-devel bzip2-devel tokyocabinet ...
- spring boot技术干货
Spring Boot2 系列教程(一)纯 Java 搭建 SSM 项目 Spring Boot2 系列教程(二)创建 Spring Boot 项目的三种方式 Spring Boot2 系列教程(三) ...
- 帝国cms 常用标签汇总
1.列表内容标签 [!--empirenews.listtemp--]<!--list.var1-->[!--empirenews.listtemp--] 2.分页标签 [!--show. ...
- JS代码格式化
JS代码格式化也就是规范化,保留必要的换行和缩进使代码阅读起来更容易.团队协同工作时会有相应的标准,大家要保证统一的代码风格,这样在合并代码的时候才不容易出问题.通过快捷键Ctrl+Shift+F进行 ...
- react如何通过shouldComponentUpdate来减少重复渲染
转自:https://segmentfault.com/a/1190000016494335 在react开发中,经常会遇到组件重复渲染的问题,父组件一个state的变化,就会导致以该组件的所有子组件 ...