题目大意: 给你一个n × m 的图,有p种宝箱, 每个点上有一个种类为a[ i ][ j ]的宝箱,a[ i ][ j ] 的宝箱里有 a[ i ][ j ] + 1的钥匙,第一种宝箱是没有锁的,

第p类宝箱只有一个且里面由宝藏,你现在在(1 ,1)问你最少需要多少步才能拿到宝藏。 (n, m <= 300)

思路:这题真的好恶心啊。。。  我们考虑p类宝箱只能从p - 1类转移过来, 这样我们就能从第一类宝箱开始往后递推, 但是最坏的情况, p 类 和p - 1类,都有45000

个点, 那么复杂度就会很大。。。  所以我们要分类讨论, 对于 (p.size()) * (p + 1. size()) > n * m 的点我们在图上进行暴力转移,复杂度微n × m, 否则我们直接暴力

转移。。。

 #include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int>>
using namespace std; const int N=+;
const int M=1e4+;
const int inf=0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9 + ; vector<pair<int, pii>> vec[N * N];
int n, m, p, idx, a[N][N], S, T, f[N * N], hs[N][N], vis[N][N];
bool flag[N][N];
int dx[] = {, -, , }, dy[] = { , , , -}; int dis(pii &a, pii &b) {
return abs(a.fi - b.fi) + abs(a.se - b.se);
} bool check(int x, int y) {
if(x < || x > n) return false;
if(y < || y > m) return false;
return true;
} void bfs(int p) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
flag[i][j] = false;
vis[i][j] = inf;
}
}
queue<piii> Q;
for(auto u : vec[p]) {
Q.push(mk(f[u.fi], u.se));
vis[u.se.fi][u.se.se] = f[u.fi];
} while(!Q.empty()) {
piii u = Q.front(); Q.pop();
for(int i = ; i < ; i++) {
piii nx = mk(vis[u.se.fi][u.se.se] + , mk(u.se.fi + dx[i], u.se.se + dy[i]));
if(check(nx.se.fi, nx.se.se)) {
if(vis[nx.se.fi][nx.se.se] > nx.fi)
{
vis[nx.se.fi][nx.se.se] = nx.fi;
if(!flag[nx.se.fi][nx.se.se]) {
flag[nx.se.fi][nx.se.se] = true;
Q.push(nx);
}
}
}
}
flag[u.se.fi][u.se.se] = false;
} for(auto u : vec[p + ]) {
f[u.fi] = vis[u.se.fi][u.se.se];
}
} int main() {
memset(f, inf, sizeof(f)); scanf("%d%d%d", &n, &m, &p);
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
scanf("%d", &a[i][j]); for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
vec[a[i][j]].push_back(mk(++idx, mk(i, j)));
hs[i][j] = idx;
if(a[i][j] == p) {
S = ;
T = idx;
}
}
}
f[] = ;
vec[].push_back(mk(, mk(, )));
for(int i = ; i <= p; i++) {
if(vec[i - ].size() * vec[i].size() <= n * m) {
for(auto u : vec[i - ]) {
for(auto v : vec[i]) {
f[v.fi] = min(f[v.fi], f[u.fi] + dis(u.se, v.se));
}
}
} else {
bfs(i - );
}
}
printf("%d\n", f[T]);
return ;
}
/*
*/

Codeforces Round #355 (Div. 2) D. Vanya and Treasure的更多相关文章

  1. Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力

    D. Vanya and Treasure 题目连接: http://www.codeforces.com/contest/677/problem/D Description Vanya is in ...

  2. Codeforces Round #355 (Div. 2) D. Vanya and Treasure dp+分块

    题目链接: http://codeforces.com/contest/677/problem/D 题意: 让你求最短的从start->...->1->...->2->. ...

  3. Codeforces Round #355 (Div. 2) C. Vanya and Label 水题

    C. Vanya and Label 题目连接: http://www.codeforces.com/contest/677/problem/C Description While walking d ...

  4. Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题

    B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...

  5. Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题

    A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...

  6. Codeforces Round #355 (Div. 2)-B. Vanya and Food Processor,纯考思路~~

    B. Vanya and Food Processor time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. Codeforces Round #355 (Div. 2)C - Vanya and Label

    啊啊啊啊啊啊啊,真的是智障了... 这种题目,没有必要纠结来源.只要知道它的结果的导致直接原因?反正这句话就我听的懂吧... ">>"/"&" ...

  8. Codeforces Round #355 (Div. 2) B. Vanya and Food Processor

    菜菜菜!!!这么撒比的模拟题,听厂长在一边比比比了半天,自己想一想,然后纯模拟一下,中间过程检测一下,妥妥的就可以过. 题意:有N个东西要去搞碎,每个东西有一个高度,然后有一台机器支持里面可以达到的最 ...

  9. 水题 Codeforces Round #308 (Div. 2) A. Vanya and Table

    题目传送门 /* 水题:读懂题目就能做 */ #include <cstdio> #include <iostream> #include <algorithm> ...

随机推荐

  1. ZOJ1100 Mondriaan's Dream

    题目链接:QAQ 大致题意:有一个m行n列的矩阵,用1*2的骨牌(可横放或竖放)完全覆盖,骨牌不能重叠,有多少种不同的覆盖的方法? Solution: \(n,m\le11\),肯定是不能暴力的,又类 ...

  2. python里使用正则表达式的非贪婪模式

    在正则表达式里,什么是正则表达式的贪婪与非贪婪匹配 如:String str="abcaxc"; Patter p="ab*c"; 贪婪匹配:正则表达式一般趋向 ...

  3. 解题:NOI 2010 航空管制

    题面 常见的套路与不常见的套路 第一问是常见的套路,建反边用优先队列跑拓扑排序 第二问是不常见的套路,如何判断一个点最早什么时候起飞?先不加它来拓扑排序,直到拓扑排序不能进行下去了,这个时刻就是它必须 ...

  4. 界面编程之QT窗口系统20180726

    /*******************************************************************************************/ 一.坐标系统 ...

  5. Redis的持久化数据

    Redis支持两种持久化:RDB和AOF模式 一.名词解释: RDB:持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot).AOF:持久化记录服务器执行的 ...

  6. python数字转换为字符串的两种方式

    主要包括两种形式进行转换: 第一种是str(),将值转换为用户便于阅读的形式: 另一种是repr(),将值转换为合法的python表达式. >>> print repr(" ...

  7. python中的无参装饰器和有参装饰器

    python中的无参装饰器和有参装饰器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 装饰器特点: 1>.开放封闭原则,即对扩展是开放的,对修改时封闭的: 2>.装饰器 ...

  8. Kafka 0.8 NIO通信机制

    一.Kafka通信机制的整体结构 同时,这也是SEDA多线程模型. 对于broker来说,客户端连接数量有限,不会频繁新建大量连接.因此一个Acceptor thread线程处理新建连接绰绰有余. K ...

  9. 前端必须掌握的30个CSS选择器

    也许你已经学会了CSS的三个简单常用的选择器:#ID,.class,标签选择器,可是这些就足够了吗?随着CSS3的到来,作为前端开发者需要掌握下面三十个基本的选择器,这样才可以在平时开发中得心用手. ...

  10. Spring RedisTemplate操作-xml配置(1)

    网上没能找到全的spring redistemplate操作例子,故特意化了点时间做了接口调用练习,基本包含了所有redistemplate方法. 该操作例子是个系列,该片为spring xml配置, ...