FatMouse and Cheese
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 87 Accepted Submission(s) : 44
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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.
Input
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.
Output
Sample Input
3 1 1 2 5 10 11 6 12 12 7 -1 -1
Sample Output
37
这道题与正常bfs不同的是在于行走时可以走1-k格 并且只能走比上一格大的地方,就把这些地方压入队列里面就好了。
/*#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
int ma[555][555];
int xx,yy;
int kk;
int dp[555][555];
int x1[4]={1,0,0,-1};
int y11[4]={0,1,-1,0};
struct point{
int x;
int y;
};
bool check(int x,int y){
if(ma[x][y]==0||x<1||x>xx||y<1||y>yy)
return 0;
return 1;
}
int bfs(int xs,int ys){
queue<point> lss;
point tt;
dp[xs][ys]=ma[xs][ys];
tt.x=xs;
tt.y=ys;
lss.push(tt);
int ans=-1;
while(!lss.empty()){
tt=lss.front();
lss.pop();
if(dp[tt.x][tt.y]>ans)
{
ans=dp[tt.x][tt.y];
}
for(int j=0;j<4;j++){
for(int i=1;i<=kk;i++)
{
int xxx=tt.x+x1[j]*i;
int yyy=tt.y+y11[j]*i;
if(xxx>=1&&xxx<=xx&&yyy>=1&&yyy<=xx&&ma[xxx][yyy]>ma[tt.x][tt.y])
{
if(dp[xxx][yyy]<ma[xxx][yyy]+dp[tt.x][tt.y])
{
dp[xxx][yyy]=ma[xxx][yyy]+dp[tt.x][tt.y];
point hh;
hh.x=xxx;
hh.y=yyy;
lss.push(hh);
}
}
}
}
}
return ans;
}
int main(){
while(scanf("%d%d",&xx,&kk)!=EOF){
if(xx==-1||kk==-1)break;
memset(dp,0,sizeof(dp));
int i,j;
for(i=1;i<=xx;i++)
{
for(j=1;j<=xx;j++)
{
scanf("%d",&ma[i][j]);
}
}
int ans=bfs(1,1);
printf("%d\n",ans);
}
return 0;
}*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,k;
int ma[555][555];
int dp[555][555];
int xd[4]={0,0,1,-1};
int yd[4]={1,-1,0,0};
int dfs(int x,int y)
{ int i,j;
int ans=0;
if(!dp[x][y])
{
for(j=1;j<=k;j++)
for(i=0;i<4;i++)
{
int xxx=x+xd[i]*j;
int yyy=y+yd[i]*j;
if(xxx<1||xxx>n||yyy>n||yyy<1)
continue;
if(ma[xxx][yyy]>ma[x][y])
ans=max(ans,dfs(xxx,yyy));
}
dp[x][y]=ans+ma[x][y];
}
return dp[x][y];
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
if(n==-1||k==-1)break;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&ma[i][j]);
memset(dp,0,sizeof(dp));
int ans=dfs(1,1);
cout<<ans<<endl;
}
return 0;
}