55分TLE求助

P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles

panjingyu @ 2020-03-31 10:42:02

#include<bits/stdc++.h>
using namespace std;
int n;
int a[1000][1000];
int dfs(int x,int y){
    if(x==n-1) return a[x][y];
    return max(a[x][y]+dfs(x+1,y),a[x][y]+dfs(x+1,y+1));
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<=i;j++){
            cin>>a[i][j];
        }
    }
    cout<<dfs(0,0)<<endl;
    return 0;
}

by IntrepidStrayer @ 2020-03-31 10:43:22

很明显要记忆化


by Arthur_Sniper @ 2020-03-31 10:43:24

要用动态规划


by liqingyang @ 2020-03-31 10:43:47

这道题是动态规划


by xhQYm @ 2020-03-31 10:43:49

爆搜可海星


by IntrepidStrayer @ 2020-03-31 10:44:30

@panjingyu 加记忆化应该就能过


by panjingyu @ 2020-03-31 11:04:07

谢谢大佬们


by panjingyu @ 2020-03-31 11:08:54

@fhh_orz 记忆化是什么?


by xhQYm @ 2020-03-31 11:12:55

@panjingyu 记忆化搜索,自己上网查。


by xhQYm @ 2020-03-31 11:13:17

记住一句真理:百度真香。


|