方格取数

dp(记忆化搜索qwq)

这道题目本想练一下记忆化搜索,但是发现自己漏洞百出。

主要存在的问题:1、如果已经判断过边界了就没必要在终点位置return;
2、访问标记要注意用法qwq

CODE:

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#define maxn 101
#define int long long
#define SZJ signed
#include<time.h>
#define AK main
#define half (l+r)>>1
#define SDOI () 
using namespace std;
#define rep(i,a,b) for (int i=a;i<=b;++i) 
#define erpe (i,a) for (int i=head[a];i!=-1;i=e[i].next)
int n,m,val[maxn][maxn],dp[20][20][20][20];
bool vis[maxn][maxn];
inline int MAX(int a,int b,int c,int d){ return max(max(max(a,b),c),d);}
inline int dfs(int fx,int fy,int sx,int sy)
{
	
	if (fx>n||fy>n||sx>n||sy>n) return 0;
	if (~dp[fx][fy][sx][sy]) return dp[fx][fy][sx][sy];
	int mx=0,tmp=0,flag1=0,flag2=0;
	if (!vis[fx][fy])
	{
		vis[fx][fy]=1;
		tmp+=val[fx][fy];
		flag1=1;
	}
	if (!vis[sx][sy])
	{
		vis[sx][sy]=1;
		tmp+=val[sx][sy];
		flag2=1;
	}
	mx=MAX(dfs(fx+1,fy,sx+1,sy),dfs(fx+1,fy,sx,sy+1),dfs(fx,fy+1,sx+1,sy),dfs(fx,fy+1,sx,sy+1));注意转移
	mx+=tmp;
	dp[fx][fy][sx][sy]=mx;
	if (flag1) vis[fx][fy]=0; //注意这样才能清空
	if (flag2) vis[sx][sy]=0;
	return mx;
}
SZJ AK SDOI
{
	cin>>n;
	memset(dp,-1,sizeof(dp));
	while (1)
	{
		int x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		if (x==0&&y==0&&z==0) break;
		val[x][y]=z;
	}
	cout<<dfs(1,1,1,1);
	return 0;
}
原文地址:https://www.cnblogs.com/bullshit/p/9904647.html