Course Notes - Digital Triangle

dp in this class. It's really dp. ;w;

1. Description of the topic

【Title name, source】

 digital triangle ioi94-1

【Problem Description】

digital triangle

                7

               3 8

              8 1 0

             2 7 4 4

            4 5 2 6 5

 

             (Figure 3.1-1)

  (Figure 3.1-1) shows a digital triangle. Please write a program to calculate a path from top to bottom somewhere

path to maximize the sum of the numbers traversed by the path.

 ●Each step can go down the left slash or the right slash;

 1<the number of triangle rows≤100;

 ●The numbers in the triangle are integers 0, 1, ... 99;

 

Input data:

The first thing to read from the INPUT.TXT file is the line number of the triangle.

In the example INPUT.TXT is represented as follows:

5

7

3 8

8 1 0

2 7 4 4

4 5 2 6 5

 

Output Data:

Write the largest sum (integer) to the OUTPUT.TXT file.

The above example is:

30

 2 3

4 5 6

became

1

2 3

4 5 6

Three, method and code

1. dfs (recursive)

#include<iostream>
#include<cstdio>




using namespace std;


const int N=107;
int a[N][N];
int years;
int n;

//Define function dfs
void dfs(int i,int j,int sum)
{
	if(i==n)//Whether to exit
	{
		if(sum>ans)ans=sum;
		// to the exit
		return;
	}
	//No exit, continue dps
	dfs(i+1,j,sum+a[i+1][j]);
	dfs(i+1,j+1,sum+a[i+1][j+1]);
}




intmain()
{
	cin>>n;
	
	//read data into array
	for(int i=1;i<=n;i++)
	{
		for(int j=1;i<=i;j++)
		{
			cin>>a[i][j];
		}
	}
	
	//do dfs
	dfs(1,1,a[1][1]);
	
	// output the answer
	cout<<ans<<endl;
	
	return 0;
}

summary

Too slow, repetitive, not usable. Too big, longlong can't hold.

Recursion: repeated subproblems, slow.

——————————————————————————————————————————

——————————————————————————————————————————

Improve -> record the road you have traveled, if it is the same, you will not go.

Use the table to record the solution of the sub-problem, which can be used directly in the future. obvious stage

——————————————————————————————————————————

——————————————————————————————————————————

2. Memorized search/planned search (dp idea)

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int N=107;
int a[N][N],f[N][N];
int n;

//Define function dfs
void dfs(int i,int j,int sum)
{
	if(i==n)//Whether to exit
	{
		f[i][j]=a[i][j];
		// to the exit
		return;
	}
	if(f[i+1][j]==-1)//If this location has not been searched, search it. Otherwise do not search.
	dfs(i+1,j,sum+a[i+1][j]);
	if(f[i+1][j+1]==-1)//If this position has not been searched, search it. Otherwise do not search.
	dfs(i+1,j+1,sum+a[i+1][j+1]);
	
	f[i][j]= a[i][j] + f[i+1][j]>f[i+1][j+1] ? f[i+1][j]:f[ i+1][j+1];//Update the maximum sum going down from itself
}


intmain()
{
	memset(f,-1,sizeof(f));//Data initialization, all have not been searched
	cin>>n;
	
	//read data into array
	for(int i=1;i<=n;i++)
	{
		for(int j=1;i<=i;j++)
		{
			cin>>a[i][j];
		}
	}
	
	f[1][1]=a[1][1];
	
	//do dfs
	dfs(1,1,a[1][1]);
	
	// output the answer
	cout<<f[1][1]<<endl;
	
	return 0;
}
 
 

3. Backward push and forward push

倒推: f[i][j]=max(f[i+1][j],f[i+1][j+1])+a[i][j]

Add from bottom to top, record the largest.


The same is true.

soul painter hahahaha

Fourth, I will say two more words, just two sentences

1.

Analyze recursion and write it out with recursion.

dp can be seen as a kind of recursion. (Freud's algorithm also counts)

2.

dp, the multi-stage decision problem,funnyEfficient.

Often used to do the shortest path problem. But only for the kind with obvious stage *, otherwise it's still Dijkstra Dijkstra, Floyd Freud.

It's so low that you can't type an English name.

*The obvious stage is that you don't go back. Don't a to d suddenly there is a way from d to a.

3.

Do more questions for dp...you will understand if you do more questions. qwq

There are template questions on Luogu Shangla.

4.

This class is about coordinate dp, and the next class is about linear dp. (It's Saturday. It's 18/4/18.)

net. shiyan. cn

oier,123456

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324528367&siteId=291194637