【P - FatMouse and Cheese】

80 篇文章 0 订阅
80 篇文章 0 订阅

思路:

  • 记忆化搜索,注意本代码成立的前提是“He eats up the cheese where he stands and then runs either horizontally or vertically to another location.” 即每次行动时只能选定一个方向猛冲。

代码:

  • 140ms 1492kB
//140ms		1492kB 


#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int mt[4][2] = {1,0 , -1,0 , 0,1 , 0,-1};
const int maxn = 105;

int N,K;
int A[maxn][maxn];
int dp[maxn][maxn];

bool CHECK(int x,int y){
	if(x>=1 && x<=N && y>=1 && y<=N)
		return true;
	return false;
}

int dfs(int x,int y){
	if(dp[x][y])
		return dp[x][y];
	int MAX = 0;
	for(int i=0;i<4;i++){
		for(int j=1;j<=K;j++){
			int nx = x + mt[i][0]*j;
			int ny = y + mt[i][1]*j;
			if(CHECK(nx,ny) && A[nx][ny]>A[x][y])
				MAX = max(MAX , dfs(nx,ny));
		}
	}
	return dp[x][y] = MAX + A[x][y];
}

int main(){
	while(cin>>N>>K && N != -1){
		for(int i=1;i<=N;i++)
			for(int j=1;j<=N;j++)
				cin>>A[i][j];
		memset(dp , 0 , sizeof(dp));
		cout<<dfs(1,1)<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值