codeforces 677 D. Vanya and Treasure
来源:互联网 发布:艾利丹尼森管培生 知乎 编辑:程序博客网 时间:2024/05/17 15:02
原创地址: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)的箱子时,才去更新。其实纵横线的意义可以随意设定了,想法还是很妙的。
#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);}elsedis[r][k]=min(dis[r][k],ans[r][c]+abs(k-c));}}cout<<ans[bx[p][0].first][bx[p][0].second];}
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 32 1 1 11 1 1 12 1 1 3
5
3 3 91 3 58 9 74 6 2
22
3 4 121 2 3 48 7 6 59 10 11 12
11
- codeforces 677 D. Vanya and Treasure
- 【12.78%】【codeforces 677D】Vanya and Treasure
- Codeforces 677D Vanya and Treasure(DP+分治) category:
- Codeforces 677D Vanya and Treasure【dp+极限思维剪枝】
- Codeforces 677D Vanya and Treasure BFS+DP (分段)
- codeforce 677D. Vanya and Treasure bfs
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure
- Codeforces Round #355 (Div. 2)D. Vanya and Treasure
- [CodeForces 677D] Vanya and Treasure (二维树状数组+静态前缀最小值)
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure【分治+BFS】
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure (两种方法结合)
- codeforces round 355 div2 D Vanya and Treasure Dp 二维线段树
- CodeForces 492D Vanya and Computer Game
- Codeforces 492D Vanya and Computer Game
- CodeForces 492D Vanya and Computer Game
- Codeforces 552D Vanya and Triangles
- Codeforces 492D Vanya and Computer Game
- 使用 CAS 在 Tomcat 中实现单点登录
- eclipse @override 报错 解决
- Java 泛型-泛型类、泛型方法、泛型接口、通配符、上下限
- NopCommerce中文文档——目录
- Search a 2D Matrix(medium)
- codeforces 677 D. Vanya and Treasure
- <三维BFS搜索> POJ 2251 Dungeon Master
- Android折线图开发之Achartnegine
- Java中值传递与引用传递
- 分布式cookie-session的实现(spring-session)
- 移动端开发框架
- 安卓横竖屏切换调用onCreate问题
- Fatal signal 6 (SIGABRT) at 0x00000126 (code=0)
- javascript动画-鼠标移动到时隐藏块移出