标签:
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 100 + 10;
int a[maxn][maxn];
int dp[maxn][maxn];
int n ,k;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,-1,1};
int dfs(int x,int y){
if(dp[x][y] != -1){
return dp[x][y];
}
int _x,_y,ans = 0;
for(int j = 1; j <= k ;++j){
for(int i = 0;i < 4 ; ++i){
_x = x + dx[i]*j;
_y = y + dy[i]*j;
if(_x < 0 || _y <0 || _x > n || _y > n || a[_x][_y] <= a[x][y]) continue;
ans = max(ans,dfs(_x,_y));
}
}
return dp[x][y] = ans + a[x][y];
}
int main(){
while(~scanf("%d%d",&n,&k) && (~n || ~k)){
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n ; ++j){
scanf("%d",&a[i][j]);
}
}
memset(dp,-1,sizeof(dp));
printf("%d\n",dfs(1,1));
}
return 0;
}
[2016-03-28][HDU][1078][FatMouse and Cheese]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/04261904cec52626a974474adc87740a.html