HDU 1078 FatMouse and Cheese(记忆化)

FatMouse and Cheese

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int n, k, dp[105][105], a[105][105];
int to[4][2] = {1,0,-1,0,0,1,0,-1};

int valid(int x, int y) {
    if(x < 1 || y < 1 || x > n || y > n)
        return 0;
    return 1;
}

int dfs(int x,int y) {
    int i, j, l, ans = 0;
    if(!dp[x][y]) {
        for(i = 1; i <= k; i++) {//跑动距离
            for(j = 0; j < 4; j++) {//4个方向
                int xx = x + to[j][0]*i;
                int yy = y + to[j][1]*i;
                if(!valid(xx, yy))
                    continue;

                if(a[xx][yy] > a[x][y])//能够拿到的最大cheese
                    ans = max(ans, dfs(xx,yy));
            }
        }

        dp[x][y] = ans + a[x][y];
    }

    return dp[x][y];
}

int main() {
    //freopen("data.in", "r", stdin);
    int i, j;
    while(~scanf("%d%d", &n, &k), n>0 && k>0) {
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
                scanf("%d", &a[i][j]);

        memset(dp, 0, sizeof(dp));
        printf("%d\n", dfs(1,1));
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/ccshijtgc/article/details/80832836