InputThere are several test cases. Each test case consists of
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 -1
Sample Output
37
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #include<set> 5 #include<iostream> 6 #include<map> 7 #include<stack> 8 #include<cmath> 9 #include<algorithm> 10 #define ll long long 11 using namespace std; 12 int n,k; 13 int f[4][2]={1,0,0,1,-1,0,0,-1}; 14 int dp[1000][1000],e[1000][1000]; 15 int dfs(int x,int y) 16 { 17 if(dp[x][y]) return dp[x][y]; 18 for(int i=0;i<4;i++) 19 { 20 for(int j=1;j<=k;j++) 21 { 22 int tx=x+j*f[i][0]; 23 int ty=y+j*f[i][1]; 24 if(tx>=0&&ty>=0&&tx<n&&ty<n&&e[tx][ty]>e[x][y]) 25 { 26 dp[x][y]=max(dfs(tx,ty)+e[tx][ty],dp[x][y]); 27 } 28 } 29 } 30 return dp[x][y]; 31 } 32 int main() 33 { 34 while(~scanf("%d%d",&n,&k)&&(n+k)!=-2) 35 { 36 memset(dp,0,sizeof(dp)); 37 for(int i=0;i<n;i++) 38 for(int j=0;j<n;j++) 39 scanf("%d",&e[i][j]); 40 printf("%d\n",dfs(0,0)+e[0][0]); 41 } 42 }
原文:https://www.cnblogs.com/csx-zzh/p/13708283.html