思路是 K 小的时候暴力 dp 转移,不难发现最后会停在值最大的点上不动,但是就是调不出来,求助。
#include <iostream>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
ll f[52][52][24010],h,w,k;
ll mp[55][55];
const ll dx[4]={1,0,-1,0};
const ll dy[4]={0,1,0,-1};
inline ll dfs(ll psx,ll psy,ll step,ll mx_step){
if(f[psx][psy][step]!=0) return f[psx][psy][step];
ll ret=0;
if(step==mx_step){
ret=mp[psx][psy];
for(ll i=0;i<4;i++){
ll tx=psx+dx[i],ty=psy+dy[i];
if(tx<1||tx>h||ty<1||ty>w) continue;
ret=max(ret,mp[tx][ty]);
}
}
else{
ret=mp[psx][psy]+dfs(psx,psy,step+1,mx_step);
for(ll i=0;i<4;i++){
ll tx=psx+dx[i],ty=psy+dy[i];
if(tx<1||tx>h||ty<1||ty>w) continue;
ret=max(ret,mp[tx][ty]+dfs(tx,ty,step+1,mx_step));
}
}
return f[psx][psy][step]=ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>h>>w>>k;
ll bgx,bgy,mx=-1;
cin>>bgx>>bgy;
for(ll i=1;i<=h;i++){
for(ll j=1;j<=w;j++){
cin>>mp[i][j];
mx=max(mx,mp[i][j]);
}
}
ll ans=dfs(bgx,bgy,1,min(24005LL,k));
if(k>24005LL) ans+=(k-24005LL)*mx;
cout<<ans;
return 0;
}