Hdu 2118

A greedy mouse cici ate rat poison by mistake. To save herself, she have to drink.

Cici can only walk in four ways that marked in the photoes followed and only move in the region that colored blue..She expend energy every step.There are cheese in every position which cici can eat to gain energy once she is there.He will never reach the river if she run out of energy.The initially energy of cici is energy of the cheese at the coordinate (0,0).
if when the mouse arrive a position has 0 energy , he can’t eat the cheese.
Now can you help the wretched monse to calculate whether can he get to the river.
If she can,calculate the minimun energy of the maximal energy of every possible position of the river(Y = N-1).

输入:

There are several test cases.In each case,there are three integers n(0<n<=200),m(0<m<=10),and k(0<k<=10)in the first line ,the next n lines followed. The second line has only one integer c(0<c<=100),the i-th line has m integers more than (i-1)th line.
k is the energy each step cost.

输出:

There are several test cases.In each case,there are three integers n(0<n<=200),m(0<m<=10),and k(0<k<=10)in the first line ,the next n lines followed. The second line has only one integer c(0<c<=100),the i-th line has m integers more than (i-1)th line.
k is the energy each step cost.

样例输入:

7 2 3
5
3 6 4
6 9 2 5 6
2 5 3 7 1 6 7
8 9 3 2 6 3 8 9 5
3 5 3 6 4 3 5 6 8 4 2
2 6 3 3 6 7 8 5 3 1 4 5 6

样例输出:

11


对于这题笔者在看完网上正解之后;

想法就是:用dp的方法对于每个点得到一个最优解,然后遍历的方式求出一个满足条件的最优解;

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int energy[205][2105];
int f[205][2105];
int dx[4]={1,0,1,2};
int dy[4]={0,1,2,1};
int n,m,K;

void dp()
{
    int i,j,k;
    f[1][1]=energy[1][1];

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=(i-1)*m+1;j++)if(f[i][j]-K>0)
        {
            for(k=0;k<4;k++)
            {
                int xx=i+dx[k];
                int yy=j+dy[k];
                if(yy<=(xx-1)*m+1&&(f[xx][yy]==-1||f[xx][yy]<f[i][j]-K+energy[xx][yy]))
                {
                    f[xx][yy]=f[i][j]-K+energy[xx][yy];
                }
            }
        }
    }

    int ans=-1;
    for(i=1;i<=(n-1)*m+1;i++)
    {
        if(f[n][i]>0&&(ans<0||ans>f[n][i]))ans=f[n][i];
    }

    if(ans>0)printf("%d\n",ans);
    else printf("what a pity mouse!!\n");
}

int main()
{
    int i,j;
    while(scanf("%d %d %d",&n,&m,&K)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=(i-1)*m+1;j++)
            {
                scanf("%d",&energy[i][j]);
                f[i][j]=-1;
            }
        }
        dp();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值