Triangle problem: A disadvantage of Dfs.

Source: Internet
Author: User

There is a triangle number array, the number of rows and columns are equal, the nth row has n numbers, now from the top vertex, that is, the first row of rows, only left down or to the right down to the next line, until the end, to find out how to make the path of the number and maximum, the maximum value.

The first line is a number m, which represents the number of tests; For each test: the first row is a number n (1<n<100), which represents the array of n rows, followed by a triangular array of n rows (1<aij<100 for each number).

For example: Enter:

1

5

7

3 8

8 1 0

2 7 4 4

4 5 2) 6 5

Output:

30

For this problem, it is easy to think of Dfs, a path to find a path to go down, and finally get that maximum value, so write out the following program:

#include <stdio.h>
int a[100][100]={0},i,j,k,m,n;
int Max=0,ans;
void dfs (int x,int y, int ans)
{
Ans+=a[x][y];
if (x==n-1)
    {
if (Max<ans)
Max=ans;
    }
Else
    {
DFS (X+1,y,ans);
DFS (X+1,y+1,ans);
    }
}
Main ()
{
scanf ("%d", &m);
While (m--)
    {
scanf ("%d", &n);
For (i=0;i<n;i++)
        {
For (j=0;j<=i;j++)
           {
scanf ("%d", &a[i][j]);
           }
        }
DFS (0,0,0);
printf ("%d\n", max);
     }
}

Then take the sample and find it right, but ignore it: time. Yes, if you test a sample with 99 rows, you will find that the results will pop up very slowly, why?

Mathematical method to calculate will know: 99 rows of the sample will have a total of 2 98 of the path, so many paths, for the computer is also relatively large, not to mention to find the maximum value,

It's not even easy!

So how do you do this problem? If you want to find the maximum value, it must go to each position has a maximum value, from the top to find the maximum value is not good to find it why not go straight from the last line to find it?

In addition to storing array of arrays, and then defining an array, there are also n rows, but each position is stored backwards to the current maximum value, so that layer by layer upward, the number of the first row of the array

Must be the maximum value of all the paths;

For example:

For:

The 7 path maximum number array is: 30

3 8 23 21

8 1 0 20 13 10

2 7 4 4 7 12 10 10

4 5 2 6 5 4 5 2 6 5

With this idea, it is easy to write the following procedure:

#include <stdio.h>
int a[100][100]={0},b[100][100],i,j,k,m,n;
int max (int x,int y)
{
if (x>y)
return x;
Else
return y;
}
void D (int n)
{
For (i=0;i<n;i++)
B[n-1][i]=a[n-1][i];
For (i=n-2;i>=0;i--)
    {
For (j=i;j>=0;j--)
B[i][j]=a[i][j]+max (b[i+1][j],b[i+1][j+1]);
     }
}
Main ()
{
scanf ("%d", &m);
While (m--)
    {
scanf ("%d", &n);
For (i=0;i<n;i++)
    {
For (j=0;j<=i;j++)
        {
scanf ("%d", &a[i][j]);
        }
     }
d (n);
printf ("%d\n", B[0][0]);
    }
}

As you can see, for this program, even if there are 100 rows, you only need to operate 4,950 times, the array B fills up, you can get the answer. Multi-use DFS will find that

With Dfs very easy time overrun, at this point, it is necessary to use other algorithms to replace it, so it is best to decide at the outset whether to use DFS, so as not to waste time!!

Triangle problem: A disadvantage of Dfs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.