hdu 1514 Free Candies 解題報告

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   2014   

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1514

題目意思:有4堆糖果,每堆糖果有n個,從上到下排好,取糖果只能從上往下取,取完的糖果放在籃子裡,籃子裡最多放5個,如果籃子裡有兩個顏色相同的糖果則可以取走放進口袋裡,問最多能取走多少對糖果放進口袋。n<=40, 糖果顏色最多20種。

     這題是抄這個人滴:http://fudq.blog.163.com/blog/static/1913502382014239225290/

     有些地方看得不太懂,本來想搜狀態壓縮DP來體會下的,陰差陽錯、迷迷糊糊找到了這題= =.......可能水平不太夠啦,先留著吧.......能理解到的我都寫注釋了,不能理解的打了個“?”

     只能說,DP好抽象啊,淚 >_< ,慢慢來吧,繼續努力!!!

    

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <set> 6 using namespace std; 7  8 const int candy_num = 40 + 5; 9 const int basket_num = 5 + 2;10 11 // dp[i][j][k][l]: 四個糖堆分別拿走了幾個12 //  第1個pile被取走i個,第2個pile被取走j個,第3個pile被取走k個,第4個pile被取走l個時pocket能裝入的candy數13 // h[i]:記錄第i個糖堆被取走了多少糖果14 int dp[candy_num][candy_num][candy_num][candy_num];15 int h[basket_num], f[candy_num][basket_num];16 int ans, n;17 18 set<int> S;19 set<int> ::iterator it;20 21 int check(int a)22 {23     it = S.find(a);24     if (it != S.end())   // basket裡面有顏色跟使用中色彩(a)相同的糖果,從basket裡拿出25     {26         S.erase(a);27         return 1;28     }29     S.insert(a);   // basket中沒有a這種顏色,放入basket中30     return 0;31 }32 33 int dfs(int tmp)34 {35     if (dp[h[0]][h[1]][h[2]][h[3]] != -1)   // 這個狀態搜過36         return dp[h[0]][h[1]][h[2]][h[3]];37     int tt = 0;38     for (int i = 0; i < 4; i++)   // 從四個basket中拿糖果39     {40         if (h[i] < n && S.size() < 5)41         {42             int t = check(f[h[i]][i]);43             if (S.size() < 5)44             {45                 h[i]++;   // 第i個pile裡被取走一個candy46                 tt = max(tt, dfs(tmp+t));   // ?47                 h[i]--;48             }49             tt = max(tt, tmp+t);     // ?50             if (t == 1)               51                 S.insert(f[h[i]][i]);52             else53                 S.erase(f[h[i]][i]);54         }55     }56     return dp[h[0]][h[1]][h[2]][h[3]] = tt;57 }58 59 int main()60 {61     while (scanf("%d", &n) != EOF && n)62     {63         for (int i = 0; i < n; i++)64         {65             for (int j = 0; j < 4; j++)66                 scanf("%d", &f[i][j]);67         }68 /*        for (int i = 0; i < n; i++)69         {70             for (int j = 0; j < 4; j++)71                 printf("f[%d][%d] = %d\n", i, j, f[i][j]);72         }73 */74         ans = 0;75         h[0] = h[1] = h[2] = h[3] = 0;76         memset(dp, -1, sizeof(dp));77         printf("%d\n", dfs(0));78     //    printf("dp[%d][%d][%d][%d] = %d\n", h[0], h[1], h[2], h[3], dp[h[0]][h[1]][h[2]][h[3]]);79     }80     return 0;81 }

 

     

     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.