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).
输入:
k is the energy each step cost.
输出:
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;
}