FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he’s going to enjoy his favorite food.

FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse — after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.

Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.

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,1开始走,每次可以走,1~n步,且后一步上格子的值要大于前一步,问最大和是多少。
题解:记忆化搜索,递归得出某个格子之后的最大值,然后一直到第一个格子结束。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include  <cassert>
#include   <cstdio>
#include   <vector>
#include   <string>
#include    <cmath>
#include    <queue>
#include    <stack>
#include      <set>
#include      <map>
using namespace std;
#define P(a,b,c) make_pair(a,make_pair(b,c))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef pair<int,pair<int,int> >pii;
typedef long long ll;
const ll mod = 1000000007;
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

const int maxn = 1e3+5;
int n,k;
int m[maxn][maxn];
int dp[maxn][maxn];
int dir[4][2]={0,1,1,0,0,-1,-1,0};

bool ojbk(int x,int y){
	return x>0&&x<=n&&y>0&&y<=n;
}
int ms(int xx,int yy){
	int maxx=0;
	if(dp[xx][yy])return dp[xx][yy];
	rep(i,0,3){
		rep(j,1,k){
			int x=xx+dir[i][0]*j;
			int y=yy+dir[i][1]*j;
			if(ojbk(x,y)&&m[x][y]>m[xx][yy]){
				maxx=max(maxx,ms(x,y));
			}
		}
	}
	return dp[xx][yy]=maxx+m[xx][yy];
}
void Init(int h){
	rep(i,1,h){
		rep(j,1,h){
			dp[i][j]=0;
		}
	}
}
int main(){
	while(scanf("%d%d", &n,&k)&&(n!=-1||k!=-1)){
		Init(n);
		rep(i,1,n){
			rep(j,1,n) scanf("%d", &m[i][j]);
		}
		ms(1,1);
		printf("%d\n", dp[1][1]);
	}
	return 0;
}

发表评论

邮箱地址不会被公开。 必填项已用*标注