codeforces 677 D. Vanya and Treasure
2016-06-02 09:54
369 查看
原创地址:http://blog.csdn.net/stl112514/article/details/51563993
做法:每次可以把已经知道ans的点都拿出来,若它的位置是(r,c),则更新所有的dis[r][k]=min(dis[r][k],ans[r][c]+abs(c-k)),1<=k<=m。也就是用横线维护dis,再用纵线维护ans,ans[r][c]=min(ans[r][c],dis[k][c]+abs(c-r)),1<=k<=n,当然了,还需要维护一个vis[][]标记出所有位置当前被携带了哪把钥匙访问过,只有满足(k,c)的钥匙能打开(r,c)的箱子时,才去更新。其实纵横线的意义可以随意设定了,想法还是很妙的。
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 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.
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
output
input
output
input
output
做法:每次可以把已经知道ans的点都拿出来,若它的位置是(r,c),则更新所有的dis[r][k]=min(dis[r][k],ans[r][c]+abs(c-k)),1<=k<=m。也就是用横线维护dis,再用纵线维护ans,ans[r][c]=min(ans[r][c],dis[k][c]+abs(c-r)),1<=k<=n,当然了,还需要维护一个vis[][]标记出所有位置当前被携带了哪把钥匙访问过,只有满足(k,c)的钥匙能打开(r,c)的箱子时,才去更新。其实纵横线的意义可以随意设定了,想法还是很妙的。
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; typedef pair<int,int> pp; vector<pp>bx[300*300+10]; int vis[310][310]; int ans[310][310]; int dis[310][310]; int main() { int n,m,p; cin>>n>>m>>p; memset(ans,63,sizeof(ans)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { int t; cin>>t; bx[t].push_back(pp(i,j)); } bx[0].push_back(pp(1,1)); for(int i=0;i<=p;i++) { int len=bx[i].size(); for(int j=0;j<len;j++) { int r=bx[i][j].first,c=bx[i][j].second; if(r==1&&c==1&&ans[r][c]==0) ans[r][c]=1000000000; for(int k=1;k<=n;k++) if(vis[k][c]==i) ans[r][c]=min(ans[r][c],dis[k][c]+abs(k-r)); } for(int j=0;j<len;j++) { int r=bx[i][j].first,c=bx[i][j].second; for(int k=1;k<=m;k++) if(vis[r][k]!=i+1) { vis[r][k]=i+1; dis[r][k]=ans[r][c]+abs(k-c); } else dis[r][k]=min(dis[r][k],ans[r][c]+abs(k-c)); } } cout<<ans[bx[p][0].first][bx[p][0].second]; }
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 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.
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 58 9 7
4 6 2
output
22
input
3 4 12
1 2 3 4
8 7 6 59 10 11 12
output
11
相关文章推荐
- Mybatis使用<foreach collection="***" item="**" open="(" separator="," close=")">问题
- object c中的多态
- 2016.06.01回顾
- (selenium 小知识点)解决使用Webdrive打开Firefox不含有插件的问题(python)
- 使用CAShapeLayer与UIBezierPath画出想要的图形
- Apache+tomcat实现负载均衡
- 关于ios object-c 类别-分类 category 的静态方法与私有变量,协议 protocol 
- 天气解析
- eclipse下maven打包"错误: 编码GBK的不可映射字符"
- OSG控制帧率的方法
- iOS应用性能调优的25个建议和技巧
- iOS学习笔记06—Category和Extension
- awk实战——分组统计(允许字符下标)
- Servlet监听器/Session钝化和活化
- mysql 常用命令大全
- [Effective JavaScript 笔记]第23条:永远不要修改arguments对象
- mongodb常见问题
- JS从一组数据中找到指定的单条数据的方法
- 大型网站架构演化发展历程
- Solr中配置文件schema.xml之copyField 与multiValued用途