记忆化搜索DP:D - Explorer Space
Posted 出尘呢
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记忆化搜索DP:D - Explorer Space相关的知识,希望对你有一定的参考价值。
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
D. Explorer Space
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are wandering in the explorer space of the 2050 Conference.
The explorer space can be viewed as an undirected weighted grid graph with size n×m. The set of vertices is {(i,j)|1≤i≤n,1≤j≤m}. Two vertices (i1,j1) and (i2,j2) are connected by an edge if and only if |i1−i2|+|j1−j2|=1.
At each step, you can walk to any vertex connected by an edge with your current vertex. On each edge, there are some number of exhibits. Since you already know all the exhibits, whenever you go through an edge containing x exhibits, your boredness increases by x.
For each starting vertex (i,j), please answer the following question: What is the minimum possible boredness if you walk from (i,j) and go back to it after exactly k steps?
You can use any edge for multiple times but the boredness on those edges are also counted for multiple times. At each step, you cannot stay on your current vertex. You also cannot change direction while going through an edge. Before going back to your starting vertex (i,j) after k steps, you can visit (i,j) (or not) freely.
Input
The first line contains three integers n, m and k (2≤n,m≤500,1≤k≤20).
The j-th number (1≤j≤m−1) in the i-th line of the following n lines is the number of exibits on the edge between vertex (i,j) and vertex (i,j+1).
The j-th number (1≤j≤m) in the i-th line of the following n−1 lines is the number of exibits on the edge between vertex (i,j) and vertex (i+1,j).
The number of exhibits on each edge is an integer between 1 and 106.
Output
Output n lines with m numbers each. The j-th number in the i-th line, answerij, should be the minimum possible boredness if you walk from (i,j) and go back to it after exactly k steps.
If you cannot go back to vertex (i,j) after exactly k steps, answerij should be −1.
Examples
inputCopy
3 3 10
1 1
1 1
1 1
1 1 1
1 1 1
outputCopy
10 10 10
10 10 10
10 10 10
inputCopy
2 2 4
1
3
4 2
outputCopy
4 4
10 6
inputCopy
2 2 3
1
2
3 4
outputCopy
-1 -1
-1 -1
Note
In the first example, the answer is always 10 no matter how you walk.
In the second example, answer21=10, the path is (2,1)→(1,1)→(1,2)→(2,2)→(2,1), the boredness is 4+1+2+3=10.
——失败,参考易懂题解
dfs多次,可优化为记忆化搜索,即记录下从某点走k步最优解,以备后用。
这种打表的方式,也算一种DP
易错:前后横竖搞反!!
int dx[5]={0,0,0,-1,1},dy[5]={0,-1,1,0,0};
//前项为竖后项为横,注意!!
//1左2右3上4下
//#pragma GCC optimize(3,"Ofast","inline")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<algorithm>
//#include<string>
//#include<sstream>
#include<vector>
#include<map>
//#include<set>
//#include<ctype.h>
//#include<stack>
//#include<queue>
#ifdef LOCAL
FILE*FP=freopen("text.in","r",stdin);
//FILE*fp=freopen("text.out","w",stdout);
#endif
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define pll pair<ll,ll>
#define plll pair<ll,pll>
#define pdd pair<double,double>
#define pdi pair<double,int>
#define pid pair<int,double>
#define vi vector <int>
#define vii vector <vi>
#define vl vector<ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a))
#define _forplus(i,a,b) for( register int i=(a); i<=(b); i++)
#define forplus(i,a,b) for( register int i=(a); i<(b); i++)
#define _forsub(i,a,b) for( register int i=(a); i>=(b); i--)
#define _forauto(a,b) for(auto &(a):(b))
#define _forautome(a,b,c) for(auto (a) = (b); (a) != (c); (a)++)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define pi (acos(-1))
#define EPS 0.00000001
#define MOD 1000000007
#define fastio std::ios::sync_with_stdio(false);std::cin.tie(0);
#define N 505
int d[N][N][5],f[N][N][12];
int dx[5]={0,0,0,-1,1},dy[5]={0,-1,1,0,0};//前项为竖后项为横,注意!!
//1左2右3上4下
int n,m,k,t;
int dfs(int x,int y,int c){
if(c==0)return 0;
if(x==0||y==0||x==n+1||y==m+1)return INF;
if(f[x][y][c]!=INF)return f[x][y][c];
int mi=INF;
_forplus(i,1,4){
mi=min(mi,d[x][y][i]+dfs(x+dx[i],y+dy[i],c-1));
}
return f[x][y][c]=mi;
}
inline int solve(int i,int j,int k){
if(k%2)return -1;
else return dfs(i,j,k/2)*2;
}
int main(){
fastio
cin>>n>>m>>k;
mem(f,0x3f);
mem(d,0x3f);
_forplus(i,1,n){
_forplus(j,1,m-1){
cin>>t;//i,j——i,j+1
d[i][j][2]=t;
d[i][j+1][1]=t;
}
}
_forplus(i,1,n-1){
_forplus(j,1,m){
cin>>t;//i,j——i+1,j
d[i][j][4]=t;
d[i+1][j][3]=t;
}
}
int flag;
_forplus(i,1,n){
flag=0;
_forplus(j,1,m){
if(flag)cout<<' ';else flag=1;
cout<<solve(i,j,k);
}
cout<<'\\n';
}
return 0;
}
以上是关于记忆化搜索DP:D - Explorer Space的主要内容,如果未能解决你的问题,请参考以下文章