跳到正文

题解

题解:P3257 [JLOI2014] 天天酷跑

carryfish约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年8月11日
收录
2026年8月12日
更新
2026年11月11日
篇幅
约 3 分钟
题目
P3257

版本

  1. v2当前
  2. v1

题意:

有一个地图,你可以吃路过的金币,当然,你可以进行一些跳跃,还可以连跳,虽然有最大连跳次数。求你能吃多少金币。

思路:

看到跳跃高度和连跳次数如此之小的范围,就知道枚举即可,然后使用记忆化搜索,时间复杂度不会炸。

可以在初始位置开始 DFS,然后通过记忆化搜索数组记录结果,通过这种方式剪枝可以让复杂度优化很多。然后对于得出的答案,我们将他减去升级所需的费用,就是这个跳跃升级情况所能得到的最大收益了。

AC code:

C++
#include<bits/stdc++.h>
using namespace std;
const int N=100010,M=25;
const long long INF=1e18;
long long f[M][N][6],res=-INF,res1,res2;
int a[M][N],n,m,pay1,pay2,c,h;
long long dfs(int x,int y,int num){
	if(y>n)return 0;
	if(a[x][y]==-1)return -INF;
	if(f[x][y][num]!=-1)return f[x][y][num];
	if(x==1)num=0;
	long long ans=-INF,sum=0;
	bool flag=1;
	if(num<c){
		int i;
		for(i=1;i<h;i++){
			if(a[x+i][y+i]==-1){
				flag=0;
				break;
			}
			sum+=a[x+i][y+i];
		}
		if(flag)ans=max(ans,sum+dfs(x+h,y+h,num+1));
	}
	if(x==1)ans=max(ans,dfs(x,y+1,0));
	if(x>1)ans=max(ans,dfs(x-1,y+1,num));
	f[x][y][num]=ans+a[x][y];
	return f[x][y][num];
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m>>pay1>>pay2;
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++)cin>>a[i][j];
	}
	for(c=1;c<=5;c++){
		for(h=1;h*c<m;h++){
			memset(f,-1,sizeof f);
			long long temp=dfs(1,0,m)-(h-1)*pay1-(c-1)*pay2;
			if(temp>res)res=temp,res1=h,res2=c;
		}
	}
	if(res>0)cout<<res<<" "<<res2<<" "<<res1;
	else cout<<"mission failed";
	return 0;
}
原文