hdu FatMouse and Cheese(dp)

http://acm.hdu.edu.cn/showproblem.php?pid=1078

很好的一道题。因为它每次走到该格子的价值要比上一个格子大,所以在dp之前应该先对map[i][j]按价值排序,再开个标记数组标记某点是否可以到达。


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
int dp[110][110],map[110][110];
int vis[110][110];

struct node
{
    int x,y;
    int data;
    bool operator < (const struct node &tmp)const
    {
        return data < tmp.data;
    }
}point[10010];

int main()
{
    int n,k;
    while(~scanf("%d %d",&n,&k))
    {
        if(n == -1 && k == -1) break;
        int cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1;j <= n; j++)
            {
                scanf("%d",&map[i][j]);
                point[++cnt] = (struct node){i,j,map[i][j]};
            }
        }
        sort(point+1,point+1+cnt);
        memset(vis,0,sizeof(vis));
        memset(dp,0,sizeof(dp));
        int x,y,ans,res;
        dp[1][1] = map[1][1];
        res = dp[1][1];
        for(int i = 1; i <= cnt; i++)
        {
            x = point[i].x;
            y = point[i].y;
            ans = -1;

            for(int j = 1; j <= k; j++)
            {
                if(x-j>=1 && map[x-j][y] < map[x][y] && !vis[x-j][y])
                    ans = max(dp[x-j][y],ans);
                if(x+j <= n && map[x+j][y] < map[x][y] && !vis[x+j][y])
                    ans = max(dp[x+j][y],ans);
                if(y-j >= 1 && map[x][y-j] < map[x][y] && !vis[x][y-j])
                    ans = max(dp[x][y-j],ans);
                if(y+j <= n && map[x][y+j] < map[x][y] && !vis[x][y+j])
                    ans = max(dp[x][y+j],ans);
            }

            if(ans == -1)
            {
                if(x == 1 && y == 1)
                    continue;
                else
                {
                    vis[x][y] = 1;
                    dp[x][y] = 0;
                    continue;
                }
            }

            dp[x][y] = map[x][y]+ans;
            res = max(res,dp[x][y]);
        }
        printf("%d\n",res);

    }
    return 0;

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值