25分求救
查看原帖
25分求救
713060
wanweijie楼主2023/10/15 14:04

见代码,目前只有25分,怎么进行记忆化使得时间不会超过1s

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const ll MAXN = 1e3 + 10;
ll n, m, ans = -1e9, now_ans = 0;
ll a[MAXN][MAXN];
bool visited[MAXN][MAXN];
void dfs(ll x, ll y) {
    if (x < 1 || x > n || y < 1 || y > m || visited[x][y]) {
        return;
    }
    if (x == n && y == m) {
    	now_ans += a[x][y];
        ans = max(ans, now_ans);
		now_ans -= a[x][y];        
        return;
    }
    now_ans += a[x][y];
    visited[x][y] = true;
    dfs(x + 1, y);
    dfs(x, y + 1);
    dfs(x - 1, y);
    visited[x][y] = false;
    now_ans -= a[x][y];
}
int main(){
	//freopen("number.in", "r", stdin);
	//freopen("number.out", "w", stdout);
	memset(visited, false, sizeof(visited));
	scanf("%lld%lld", &n, &m);
	for(ll i = 1;i <= n; ++i) {
		for(ll j = 1; j <= m; ++j) {
			scanf("%lld", &a[i][j]);
		}
	}
	dfs(1,1);
	printf("%lld", ans);
	return 0;
}
2023/10/15 14:04
加载中...