DFS與DP算法


名詞解釋:

DFS(Dynamic Plan):動態規划

DFS(Depth First Search):深度優先搜索

DFS與DP的關系

很多情況下,dfs和dp兩種解題方法的思路都是很相似的,這兩種算法在一定程度上是可以互相轉化的。

想到dfs也就常常會想到dp,當然在一些特定的適用場合下除外。

dp主要運用了預處理的思想,而dfs則是類似於白手起家,一步步探索。一般來講,能夠預處理要好些,好比戰爭前做好准備。

dfs和dp都是十分重要的基礎算法,在各個競賽中都有涉及,務必精通。


題目:

The Triangle

 

描述:

編寫一個程序,計算從頂部開始到底部某處的路徑上傳遞的最大數字總和。 每個步驟可以向左下或右下滑動。

輸入:

程序是從標准輸入讀取。 第一行包含一個整數N:三角形中的行數。 以下N行描述了三角形的數據。 三角形中的行數1<N<=100.三角形中的數字全部為整數,介於0到99之間。

輸出:

程序是標准輸出。輸出最大的整數和。

 樣例:

輸入:

輸出:


 

DFS思路:

自頂向下,將每種路徑都走一遍。

通過迭代計算到最后一層,記錄最后一層的所有值。最后一層中的最大值即為所求。

具體代碼:

 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM