标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7576 Accepted Submission(s): 3133
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 int n,k; 9 const int maxn = 105; 10 int maze[maxn][maxn],dp[maxn][maxn]; 11 int dfs(int p,int q){ 12 if(dp[p][q]) return dp[p][q]; 13 int l,r,d,u; 14 int ans = 0,maxx = 0; 15 l = p - k; 16 r = p + k; 17 if(l<0) l = 0; 18 if(r>=n) r = n-1; 19 for(int i = l; i<=r; i++){ 20 if(maze[i][q]>maze[p][q]){ 21 ans = dfs(i,q); 22 if(ans>maxx) maxx = ans; 23 } 24 } 25 u = q + k; 26 d = q - k; 27 if(d<0) d = 0; 28 if(u>=n) u = n-1; 29 for(int i = d; i<=u; i++){ 30 if(maze[p][i]>maze[p][q]){ 31 ans = dfs(p,i); 32 if(ans>maxx) maxx = ans; 33 } 34 } 35 dp[p][q] = maxx + maze[p][q]; 36 return dp[p][q]; 37 } 38 void input(){ 39 while(scanf("%d%d",&n,&k)!=EOF&&n != -1&&k != -1){ 40 for(int i = 0; i<n; i++) 41 for(int j = 0; j<n; j++) 42 scanf("%d",&maze[i][j]); 43 memset(dp,0,sizeof(dp)); 44 printf("%d\n",dfs(0,0)); 45 } 46 } 47 int main() 48 { 49 input(); 50 return 0; 51 }
标签:
原文地址:http://www.cnblogs.com/littlepear/p/5385164.html