/*****************************************
Author :Crazy_AC(JamesQi)
Time :2015
File Name :
思路:dfs + dp,,,因为只能走直线,和普通dfs一样的方向dx[i]与dy[i],由于一次最多跳k步长元,所以有nx = dx[i] * jamp + x,ny = dy[i] * jamp + y;
状态转移方程:dp[x][y] = gg[x][y] + MAX;
MAX = get_Max(MAX,ans);
ans = dfs(...);
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long ll;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int gg[110][110];
int dp[110][110];
int dx[] = {-1,0,1,0};
int dy[] = {0,-1,0,1};
int n,k;
bool check(int x,int y){
return x >= 0 && x < n && y >= 0 && y < n;
}
int dfs(int x,int y){
if (dp[x][y] > 0) return dp[x][y];
int MAX = 0;
for (int i = 0;i < 4;i++){
for (int jamp = 1;jamp <= k;++jamp){
int nx = x + dx[i] * jamp;
int ny = y + dy[i] * jamp;
if (check(nx,ny) && gg[nx][ny] > gg[x][y]){
int ans = dfs(nx,ny);
if (MAX < ans){
MAX = ans;
}
}
}
}
return dp[x][y] = MAX + gg[x][y];
}
int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&k) && k != -1){
for (int i = 0;i < n;++i)
for (int j = 0;j < n;++j){
scanf("%d",&gg[i][j]);
dp[i][j] = 0;
}
dfs(0,0);
cout << dp[0][0] << endl;
}
return 0;
}
hdu1078基础dp
最新推荐文章于 2020-03-29 12:24:17 发布