题目戳我
本题的意思是一只老鼠再一个n*n的图上找东西吃每次只能走k个单位距离,每次获得的奶酪要比上一次多,求不能走时最大奶酪数量。
本来想像以前的dp一样的写的,但是感觉好难写啊,然后就想用记忆化搜索,问题又来了我还是太菜了,调了半天还是没调出来,总是想到算法写不出来好难受啊。
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<stdlib.h>
#include<set>
#include<iomanip>
#include<stack>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define se second
#define bug cout<<"----acac----"<<endl
#define IOS ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
using namespace std;
const int maxn = 3e5 + 50;
const int maxm = 1.5e5+50;
const double eps = 1e-7;
const double inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const double pi=3.141592653589;
int dir[][2]={{-1,0},{1,0},{0,1},{0,-1}};
int a[105][105];
int dp[105][105];
int n,k;
int dfs(int x,int y)
{
if(dp[x][y])return dp[x][y];
int ans=0,maxx=0;
for(int i=1;i<=k;i++)
{
for(int j=0;j<4;j++)
{
int dx=x+dir[j][0]*i;
int dy=y+dir[j][1]*i;//控制向四周走k个单位
if(dx<1||dy<1||dx>n||dy>n||a[dx][dy]<=a[x][y])continue;
ans=dfs(dx,dy);
maxx=max(maxx,ans);
}
} dp[x][y]=maxx+a[x][y];
return dp[x][y];
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
if(n==-1&&k==-1)break;
ms(dp,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
//int ans=dfs(1,1,-1);
printf("%d\n", dfs(1,1));
}
return 0;
}