HDU 1078 FatMouse and Cheese(简单DP)

来源:互联网 发布:数据加密的目的是 编辑:程序博客网 时间:2024/06/06 02:21

解题思路:

 很水的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;}

0 0
原创粉丝点击