HDU 1078 FatMouse and Cheese .

本文介绍了一种解决HDU 1078问题的动态规划算法实现,通过递归的方式求解最大路径值。文章提供了完整的C++代码示例,并解释了如何遍历网格以及更新状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1078

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=100+5;
int N,k;
int G[maxn][maxn],d[maxn][maxn];
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
bool inside(int x,int y){
	return x>=0&&x<N&&y>=0&&y<N;
}
int DP(int x,int y){
	int& ans=d[x][y];
	if(ans!=0) return ans;
	
	for(int i=1;i<=k;i++)
	for(int j=0;j<4;j++){
		int nx=x+i*dx[j];
		int ny=y+i*dy[j];
		if(inside(nx,ny)&&G[nx][ny]>G[x][y])
			ans=max(ans,DP(nx,ny));
	}
	ans+=G[x][y];
	return ans;
}
int main()
{
	while(cin>>N>>k)
	{
		if(k==-1&&N==-1) break;
		for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j)
			scanf("%d",&G[i][j]);
		
		memset(d,0,sizeof(d));
		cout<<DP(0,0)<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值