Glossary:
DFS (Dynamic Plan): Dynamic Programming
DFS (Depth First Search): depth-first search
DFS's relationship with DP
In many cases, dfs ideas and two kinds of problem-solving methods dp are very similar, these two algorithms to some extent, can be transformed into each other.
Dfs think they often think of dp, of course, except in some specific application of occasions.
The main idea of using a pre-dp, and dfs is similar to starting from scratch, step by step exploration. In general, pre-treatment can be better, just like before the war to prepare.
Basic algorithm dfs and dp are very important in various competitions are involved, be sure to master.
topic:
The Triangle
description:
Write a program that calculates the maximum digital sum from the top in the end somewhere in the transmission path of the section. Each step may be the lower left or lower right slider.
Input:
Program is read from stdin. The first row contains an integer N: the number of rows in the triangle. The following describes N triangular data line. The number of lines in a triangle 1 <N <= 100. All digital triangles is an integer between 0 and 99.
Output:
Program is the standard output. And output largest integer.
Example:
Input:
Output:
DFS ideas:
Top-down, each path will have to go again.
Calculated by iterating the last layer, recording all the values of the last layer. Finally, the maximum level is also desired.
Specific code:
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <functional> 5 using namespace std; 6 7 // the maximum of the triangle ordinal 8 const int max_ordinal = 100; 9 // the depth 10 int num_of_rows; 11 // save data 12 int data[max_ordinal][max_ordinal]; 13 // save the data of the final level 14 vector<int> ans; 15 16 void dfs(int level, int sum, int column) 17 { 18 // avoid multi calculate 19 int current_sum = sum+data[level][column]; 20 // save data which was in final level 21 if(level+1 == num_of_rows) 22 { 23 ans.push_back(current_sum); 24 return; 25 } 26 // binary tree 27 dfs(level+1, current_sum, column); 28 dfs(level+1, current_sum, column+1); 29 } 30 31 int main() 32 { 33 cin >> num_of_rows; 34 for(int i = 0; i < num_of_rows; i++) 35 for(int j = 0; j <= i; j++) 36 scanf("%d", &data[i][j]); 37 38 dfs(0, 0, 0); 39 cout << "output data:" << endl; 40 41 sort(ans.begin(), ans.end(), greater<int>()); 42 for(int i = 0; i < ans.size(); i++) 43 { 44 cout << ans[i] << "\t"; 45 if(!((i+1) % 5)) cout << endl; 46 } 47 cout << endl; 48 49 return 0; 50 }
DP思路:
dfs的思路是从上到下,而dp的思路是:从第二层开始,每下去一次往回看一下并取上一层相邻两个大的那个。
具体代码:
1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 using namespace std; 5 6 // same as DFS 7 const int max_ordinal = 100; 8 int num_of_rows; 9 int data[max_ordinal][max_ordinal]; 10 // the array of the dp method 11 int dp[max_ordinal][max_ordinal]; 12 13 int main() 14 { 15 cin >> num_of_rows; 16 for(int i = 0; i < num_of_rows; i++) 17 for(int j = 0; j<= i; j++) 18 scanf("%d", &data[i][j]); 19 20 // dp now 21 dp[0][0] = data[0][0]; 22 for(int i = 1; i < num_of_rows; i++) 23 { 24 for(int j = 0; j <= i; j++) 25 { 26 if(j-1 >= 0) 27 { 28 dp[i][j] = data[i][j] + max(dp[i-1][j], dp[i-1][j-1]); 29 } else { 30 dp[i][j] = data[i][j] + dp[i-1][j]; 31 } 32 } 33 } 34 35 // calling 'sort' method 36 sort(dp[num_of_rows-1], &dp[num_of_rows-1][num_of_rows], greater<int>()); 37 for(int i = 0; i < num_of_rows; i++) 38 cout << dp[num_of_rows-1][i] << " "; 39 cout << endl; 40 cout << "answer is: "; 41 cout << dp[num_of_rows-1][0] << endl; 42 43 return 0; 44 }