题目:
FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse -- after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.
Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.
a line containing two integers between 1 and 100: n and k
n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) ... (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), ... (1,n-1), and so on.
The input ends with a pair of -1's.
OutputFor each test case output in a line the single integer giving the number of blocks of cheese collected.
Sample Input
3 1 1 2 5 10 11 6 12 12 7 -1 -1Sample Output
37
题意:
给出n*n矩阵,以及每一步能够走的步数,并且矩阵每个格子都有对应的数字表示奶酪数,求出老鼠能够吃到的最多的奶酪数,(前一个格子的奶酪数少于后一个格子的)
分析:
dp[i][j]表示在(i,j)点的时候的最大奶酪数,没说范围,那就dfs吧,好理解;
代码;
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
int dp[111][111],a[111][111];
int neext[4][2]={0,1,0,-1,1,0,-1,0};
int n,k;
int judge(int x,int y)
{
if(x<0||y<0||x>=n||y>=n)
return 1;
return 0;
}
int dfs(int x,int y)
{
int i,j,xx,yy,ans=0;
if(!dp[x][y])
{
for(i=1;i<=k;i++)是这吧每种能走的步数都试一下
{
for(j=0;j<4;j++)
{
xx=x+neext[j][0]*i;
yy=y+neext[j][1]*i;
if(!judge(xx,yy)&&a[xx][yy]>a[x][y])
ans=max(ans,dfs(xx,yy));
}
}
dp[x][y]=ans+a[x][y];
}
return dp[x][y];
}
int main()
{
int i,j,m,l,res;
while(scanf("%d %d",&n,&k)!=EOF)
{
if(n==-1&&k==-1)
break;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
memset(dp,0,sizeof(dp));
res=dfs(0,0);
printf("%d\n",res);
}
return 0;
}
贪吃鼠迷宫寻酪

本文探讨了一只老鼠在一个n×n的网格城市中寻找并吃掉尽可能多的奶酪的问题。老鼠从左上角开始,每次可以向相邻的方向移动最多k步,但必须移动到奶酪数量比当前位置更多的位置。文章通过深度优先搜索算法实现了解决方案。
1084

被折叠的 条评论
为什么被折叠?



