标签:
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <stack> #include <queue> #include <string> #include <vector> #include <algorithm> const int inf = 0x3f3f3f; const int MAXN = 1e3+10; using namespace std; int a[MAXN][MAXN]; int dp[MAXN][MAXN]; /*int mov[4][2] = {{1,1},{1,-1},{-1,1},{-1,-1}};*/ int n,k; void init(){ memset(dp,-1,sizeof(dp)); } int check(int x,int y){ if(x<0||x>=n||y<0||y>=n)return 0; else return 1; } int dfs(int x,int y){ if(dp[x][y]!=-1)return dp[x][y]; dp[x][y] = a[x][y]; int nx,ny; /*for(int i=k;i>=0;i--){ for(int j=k-i;j>=0;j--){ if(i+j==0)continue; for(int w=0;w<4;w++){ nx = x+mov[w][0]*i; ny = y+mov[w][1]*j; if(check(nx,ny)&&a[x][y]<a[nx][ny]){ //cout<<"ok"<<endl; if(dfs(nx,ny)+a[x][y]>dp[x][y]){ dp[x][y] = dp[nx][ny]+a[x][y]; } } } } }*/ //每次都是单向的 for(int i=0;i<4;i++){ // nx = x+move[j][0]; // ny = y+move[j][1]; nx = x; ny = y; for(int j=1;j<=k;j++){ if(i==0)nx = x+j; else if(i==1)nx = x-j; else if(i==2)ny = y+j; else ny = y-j; if(check(nx,ny)&&a[x][y]<a[nx][ny]){ //dfs(nx,ny,t+1); if(dfs(nx,ny)+a[x][y]>dp[x][y]){ dp[x][y] = dp[nx][ny]+a[x][y]; } } } } return dp[x][y]; } int main() { while(scanf("%d%d",&n,&k)!=EOF&&n!=-1){ init(); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ scanf("%d",&a[i][j]); } } dfs(0,0); /*for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cout<<dp[i][j]<<" "; } cout<<endl; }*/ cout<<dp[0][0]<<endl; } //cout << "Hello world!" << endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/EdsonLin/p/5479122.html