FatMouse and Cheese HDU - 1078

19 篇文章 0 订阅

题目戳我
本题的意思是一只老鼠再一个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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值