HDU 1078 FatMouse and Cheese(简单DP)

解题思路:

很水的DP,记得按照权值大小排序即可。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define LL long long
#define FOR(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
const int maxn = 100 + 10;
struct Node
{
    int x;
    int y;
    int w;
    bool operator < (const Node &rhs)const
    {
        return w < rhs.w;
    }
}nodes[maxn*maxn];
int W[maxn][maxn];
int dp[maxn][maxn];
int N, K, M;
int main()
{
    while(scanf("%d%d", &N,&K)!=EOF)
    {
        if(N == -1 && K == -1)
            break;
        M = 0;
        FOR(i,1,N)
        {
            FOR(j,1,N)
            {
                scanf("%d", &W[i][j]);
                nodes[++M].x = i;
                nodes[M].y = j;
                nodes[M].w = W[i][j];
            }
        }
        memset(dp,-1,sizeof(dp));
        dp[1][1] = W[1][1];
        int ans = -1;
        sort(nodes+1, nodes+1+M);
        FOR(i,1,M)
        {
            int x = nodes[i].x;
            int y = nodes[i].y;
            for(int xx=max(1,x-K);xx<=min(N,x+K);xx++)
            {
                if(W[x][y] > W[xx][y] && dp[xx][y] != -1)
                {
                    dp[x][y] = max(dp[x][y], dp[xx][y] + W[x][y]);
                }
            }
            for(int yy=max(1,y-K);yy<=min(N,y+K);yy++)
            {
                if(W[x][y] > W[x][yy] && dp[x][yy] != -1)
                {
                    dp[x][y] = max(dp[x][y], dp[x][yy] + W[x][y]);
                }
            }
            ans = max(ans , dp[x][y]);
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2025-01-21 08:51:54

HDU 1078 FatMouse and Cheese(简单DP)的相关文章

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 <func

HDU 1078 FatMouse and Cheese 简单记忆化搜索

题意是:给你n和k,一个老鼠从左上角开始走,每次可以往一个方向走1~k中的任何一个值,但是每一步必须比前一步的值大,问获取的最多的值是多少? 简单记忆化搜索,dp[i][j]表示当前位置能获取的最大值,但是要注意,考虑全所有的情况才能用记忆化搜索,只要没有后效性,所有dfs,我觉得理论上都能用记忆化搜索. #include <cstdio>#include <iostream>#include <vector>#include <cmath>#include

HDU 1078 FatMouse and Cheese(DP)

题意  老鼠在一个小镇吃奶酪  城镇可以看成一个n*n的矩阵  其中每个格子都有一定数量的奶酪mat[i][j]   老鼠从(0,0) 开始吃   而且下个吃的格子里的奶酪必须比上个格子多   老鼠只能水平方向或者垂直方向走  而且每次走的距离不能超过k  求老鼠最多能吃多少奶酪 起点是固定的   比较容易   直接记忆化搜索 令d[i][j]表示以(i,j)为终点的最优解  那么对于所有(i,j)能到达的点(x,y)有  d[i][j]=max(d[i][j],d[x][y]+mat[x][y

HDU 1078 FatMouse and Cheese ( DP, DFS)

HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 (0, 0) 点开始, 下一步的值必须比现在的值大. 问所能得到的最大值. 解题思路 一般的题目只允许 向下 或者 向右 走, 而这个题允许走四个方向, 所以状态转移方程为 dp(x, y) = dp(nextX, nextY) + arr(x, y); dp 代表在 x, y 的最大值. 由于 下一

[2016-03-28][HDU][1078][FatMouse and Cheese]

时间:2016-03-28 17:40:34 星期一 题目编号:[2016-03-28][HDU][1078][FatMouse and Cheese] #include <algorithm> #include <cstring> #include <cstdio> using namespace std; const int maxn = 100 + 10; int a[maxn][maxn]; int dp[maxn][maxn]; int n ,k; int d

HDU 1078 FatMouse and Cheese(记忆化)

Problem Description FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 a

HDU 1160 FatMouse&#39;s Speed 简单DP

FatMouse's Speed Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the

hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4811    Accepted Submission(s): 1945 Problem Description FatMouse has stored some cheese in a city. The city can

HDU 1078 FatMouse and Cheese(记忆化搜索)

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8610    Accepted Submission(s): 3611 Problem Description FatMouse has stored some cheese in a city. The city can be considere

HDU - 1078 FatMouse and Cheese(记忆化+dfs)

FatMouse and Cheese FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 a