zoukankan      html  css  js  c++  java
  • hdu 1078 FatMouse and Cheese【dp】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078
    题意:每次仅仅能走 横着或竖着的 1~k 个格子。求最多能吃到的奶酪。


    代码:

    
    
    #include <stdio.h>
    #include <ctime>
    #include <math.h>
    #include <limits.h>
    #include <complex>
    #include <string>
    #include <functional>
    #include <iterator>
    #include <algorithm>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <list>
    #include <bitset>
    #include <sstream>
    #include <iomanip>
    #include <fstream>
    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <cstdio>
    #include <time.h>
    #include <ctype.h>
    #include <string.h>
    #include <assert.h>
    
    using namespace std;
    
    int n, k;
    int a[110][110], dp[110][110];
    int dir[4][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
    
    bool is_ok(int x, int y)
    {
        if (x < 0 || x >= n || y < 0 || y >= n) return false;
        return true;
    }
    
    int res(int x, int y)
    {
        int sum = 0,tmp = 0;
        if (!dp[x][y])
        {
            for (int i = 1; i <= k; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    int xx = x + dir[j][0] * i;
                    int yy = y + dir[j][1] * i;
                    if (is_ok(xx,yy) && a[xx][yy] > a[x][y])
                    {
                        sum = res(xx, yy);
                        tmp = max(tmp, sum);
                    }
                }
            }
            dp[x][y] = tmp + a[x][y];
        }
        return dp[x][y];
    }
    
    int main()
    {
        while (scanf("%d %d", &n,&k) != EOF)
        {
            if (n == -1 && k == -1) break;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    scanf("%d",&a[i][j]);
            memset(dp, 0, sizeof(dp));
            printf("%d
    ", res(0,0));
        }
        return 0;
    }
    
  • 相关阅读:
    Python3-元组
    Python3-列表
    Python3-字符串
    Python3-for循环机制
    Python3-初识
    优先队列——priority queue
    单调队列 —— 滑动窗口
    SDNU_ACM_ICPC_2021_Winter_Practice_7th [个人赛]
    博弈论入门(论和威佐夫、巴什、尼姆打牌被吊打是什么感受(╥﹏╥)
    字符串最大最小表示法
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7255572.html
Copyright © 2011-2022 走看看