codeforces_677D. Vanya and Treasure(BFS+DP)
来源:互联网 发布:安兔兔网络连接失败 编辑:程序博客网 时间:2024/05/17 12:48
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
直接bfs会超时,需要对小数据dp优化。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 310#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;vector<pair<int,int> >V[maxn*maxn];int dx[4]= {0,-1,0,1};int dy[4]= {1,0,-1,0};int a[maxn][maxn];int dp[maxn][maxn];int dis[maxn][maxn];int vis[maxn][maxn];int main(){ int n,m,p; cin>>n>>m>>p; mem(dp,127); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { Si(a[i][j]); V[a[i][j]].push_back(make_pair(i,j)); if(a[i][j]==1)dp[i][j]=i-1+j-1; } queue<pair<int,int> >Q; int level=sqrt(n*m); for(int i=1; i<p; i++) { if(V[i].size()<=level) { for(int j=0;j<V[i+1].size();j++) { for(int k=0;k<V[i].size();k++) { dp[V[i+1][j].first][V[i+1][j].second]= min(dp[V[i+1][j].first][V[i+1][j].second],dp[V[i][k].first][V[i][k].second]+ abs(V[i+1][j].first-V[i][k].first)+abs(V[i+1][j].second-V[i][k].second)); } } } else { mem(dis,127); for(int j=0; j<V[i].size(); j++) { dis[V[i][j].first][V[i][j].second]=dp[V[i][j].first][V[i][j].second]; Q.push(V[i][j]); } while(!Q.empty()) { pair<int,int>now=Q.front(); Q.pop(); pair<int,int>next; for(int k=0; k<4; k++) { next.first=now.first+dx[k]; next.second=now.second+dy[k]; if(next.first>n||next.first<1)continue; if(next.second>m||next.second<1)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(a[next.first][next.second]!=i+1) { Q.push(next); } } } } for(int j=0; j<V[i+1].size(); j++) { dp[V[i+1][j].first][V[i+1][j].second]=dis[V[i+1][j].first][V[i+1][j].second]; } } } printf("%d\n",dp[V[p][0].first][V[p][0].second]); return 0;}
- codeforces_677D. Vanya and Treasure(BFS+DP)
- Codeforces 677D Vanya and Treasure BFS+DP (分段)
- codeforce 677D. Vanya and Treasure bfs
- Codeforces 677D Vanya and Treasure(DP+分治) category:
- Codeforces 677D Vanya and Treasure【dp+极限思维剪枝】
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure【分治+BFS】
- codeforces round 355 div2 D Vanya and Treasure Dp 二维线段树
- codeforces 677 D. Vanya and Treasure
- 【12.78%】【codeforces 677D】Vanya and Treasure
- 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 (两种方法结合)
- POJ1924----The Treasure----BFS
- (二分,BFS).Treasure Island
- A. Vanya and Cards
- A. Vanya and Cards
- leetcode_c++:哈希:Group Anagrams (049)
- java.sql.SQLException: Field 'id' doesn't have a default value解决方案
- 用户需求说明书 与 需求规格说明书 有什么本质区别?
- 关闭eclipse或MyEclipse的单词检查
- HDU-2263 过山车(二分图最大匹配)
- codeforces_677D. Vanya and Treasure(BFS+DP)
- java.lang.UnsupportedClassVersionError: Unsupported major.minor version 52.0
- View展开隐藏动画
- DSP、Actel转接板标准电路
- 数据库中的分页问题
- android 布局
- C#的DateTime得到特定日期
- condition update在分布式系统中设计
- POST请求报错:Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around charact