[Recursion] Digital triangle DFS memory search recursion

2192: [Recursion] Digital Triangle
Time limit: 1 Sec Memory limit: 64 MB
Title description
For most people, "We are so normal, so we are so mediocre." And geniuses are always different. So when the evil wolf asked King Shura: "Boss, you have squatted there and watched for half an hour without moving. Is the ant so good-looking?"

King Shura replied like this: "I'm thinking about the meaning of life. You see, this ant has countless roads to choose from, but it doesn't know which way to choose to reach the goal, nor does it know which way has more food. , Let alone the influence of the path chosen now on it in the future..."

As shown in the figure, there is a digital triangle with the number of layers n (n≤1000). There is an ant that starts from the top floor and walks down, and each time it walks the next level, it can walk in the lower left direction or the lower right direction. Find the maximum value of the sum of the numbers it passes after it reaches the bottom.
Insert picture description here

Enter the
first integer as n, and the following n lines are the numbers of each layer.
Output
an integer, the maximum value, which is guaranteed not to exceed the maximum range of the integer.
Sample input Copy
5
1
6 3
8 2 6
2 1 6 5
3 2 4 7 6
Sample output Copy
23
prompt
Maximum value = 1+3+6+6+7=23

	#include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    priority_queue <int,vector<int>,less<int> > q;
    priority_queue <int,vector<int>,greater<int> > pp;
    vector <int> aa;
    const int maxn=1e6+888;
    const int N=1006;
    int n,sum=0,minx=-1;
    int a[N][N];
    int b[N][N];
    int x=0,y=0;
    int dfs(int x,int y)
    {
    
    
        if(x==n-1||b[x][y])
            return a[x][y];
 
     b[x][y]=1;
     return a[x][y]+=max(dfs(x+1,y),dfs(x+1,y+1));
    }
    ll pre[maxn],sz[maxn],rk[maxn];
    ll c[maxn]={
    
    0};
    int m,v;
 
 
    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);
    }

Guess you like

Origin blog.csdn.net/qq_52172364/article/details/112722710