D. Vanya and Treasure

题目连接:

http://www.codeforces.com/contest/677/problem/D

Description

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 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.

The only line of the input contains a single word s (1 ≤ |s| ≤ 100 000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'.

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.

Sample Input

3 4 3

2 1 1 1

1 1 1 1

2 1 1 3

Sample Output

5

Hint

题意

给你一个n*m的方格,你需要把权值为p的那个门打开,前提是你必须打开过权值为p-1的门……

然后问你打开权值为p的门,你最少走的距离是多少,一开始你在(1,1)

题解:

n*mlog的就扫描线莽一波

n^3的话,就分治一下就好了,如果当前权值的数量小于sqrt(nm)的话,我就暴力更新,否则我就在全图进行一次bfs

这样均摊下来,复杂度是nmsqrt(n)sqrt(m)的

所以直接暴力吧

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 305;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int a[maxn][maxn],dp[maxn][maxn],n,m,p;
int dis[maxn][maxn],inq[maxn][maxn];
vector<pair<int,int> >V[maxn*maxn];
int main()
{
memset(dp,127,sizeof(dp));
scanf("%d%d%d",&n,&m,&p);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==1)dp[i][j]=i-1+j-1;
V[a[i][j]].push_back(make_pair(i,j));
}
}
int level = sqrt(n*m);
int idx=1;
queue<pair<int,int> >Q;
for(int i=1;i<p;i++)
{
int sz = V[i].size();
if(sz<=level){
for(auto v:V[i+1])
for(auto u:V[i])
dp[v.first][v.second]=min(dp[v.first][v.second],dp[u.first][u.second]+abs(v.first-u.first)+abs(v.second-u.second));
}
else{
idx++;
for(auto v:V[i])
Q.push(v);
memset(dis,127,sizeof(dis));
for(auto v:V[i])
dis[v.first][v.second]=dp[v.first][v.second];
while(!Q.empty())
{
pair<int,int>now=Q.front();
Q.pop();
inq[now.first][now.second]=0;
for(int k=0;k<4;k++)
{
pair<int,int>next=now;
next.first+=dx[k];
next.second+=dy[k];
if(next.first<1||next.first>n)continue;
if(next.second<1||next.second>m)continue;
if(dis[next.first][next.second]>dis[now.first][now.second]+1)
{
dis[next.first][next.second]=dis[now.first][now.second]+1;
if(inq[next.first][next.second]<idx){
inq[next.first][next.second]=idx;
Q.push(next);
}
}
}
}
for(auto v:V[i+1])
dp[v.first][v.second]=dis[v.first][v.second];
}
}
cout<<dp[V[p][0].first][V[p][0].second]<<endl;
}

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

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

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

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

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

  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. 跳出python的各种坑(1)

    2017-11-1915:38:17 一定要跳出python的各种坑,一开始遇到的好多思维上的认知错误,因为刚开始学习,对python是个什么都不清楚,所以记录一下自己遇到的各种坑.不用担心自己遇到的 ...

  2. ASP.NET Web Api OwinSelfHost Restful 使用

    一.前言 总结一下什么是RESTful架构: (1)每一个URI代表一种资源: (2)客户端和服务器之间,传递这种资源的某种表现层: (3)客户端通过四个HTTP动词,对服务器端资源进行操作,实现&q ...

  3. python网络编程--线程递归锁RLock

    一:死锁 所谓死锁:是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进 ...

  4. ASP .NET Core 2.0 MVC 发布到 IIS 上以后 无法下载apk等格式的文件

    ASP .NET Core MVC 发布到  IIS 上以后 无法下载apk等格式的文件 使用.NET Core MVC创建了一个站点,其他文件可以下载,但是后来又需求,就把手机端的apk合适的文件上 ...

  5. 开发者常用的 Sublime Text 3 插件

    1.官网下载 Sublime Text 3 (已有安装包的,请忽略) Sublime Text 官网下载地址 : http://www.sublimetext.com/ 2.打开 Sublime Te ...

  6. java 多线程总结篇4——锁机制

    在开发Java多线程应用程序中,各个线程之间由于要共享资源,必须用到锁机制.Java提供了多种多线程锁机制的实现方式,常见的有synchronized.ReentrantLock.Semaphore. ...

  7. GreenPlum学习笔记:基础知识

    一.介绍 GreenPlum分布式数据仓库,大规模并行计算技术. 无共享/MPP核心架构 Greenplum数据库软件将数据平均分布到系统的所有节点服务器上,所以节点存储每张表或表分区的部分行,所有数 ...

  8. Python 面试题学习

    Python的函数参数传递 在Python中,strings,tuples=('abc',123,2.2,'join),numbers 是不可更改的对象. list=['abc',123,2.23,' ...

  9. AutoCompleteTextView,Spinner,消息提示

    package com.example.wang.testapp2; import android.app.Notification; import android.app.NotificationM ...

  10. 跟我一起学WPF(2):WPF控件基础

    WPF控件简介 通过上一篇XAML语言的介绍,我们知道,XAML是一个树形结构,同样,WPF控件作为构成整个XAML树的一部分,也是一个树形结构.我们看一个简单的例子. <Button.Cont ...