動態規劃_百鍊1664 放蘋果

來源:互聯網
上載者:User

標籤:元素   return   開始   動態規劃   蘋果   class   nal   body   lib   

 1 #define _CRT_SECURE_NO_WARNINGS   2 #include <stdio.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <stdlib.h> 6 #include <vector> 7 #include <map> 8 #include <queue> 9 #include <string>10 #include <iostream>11 #include <ctype.h>12 #include <string.h>13 #include <set>14 #include <stack>15 #include<functional>16 using namespace std;17 #define Size 1000118 #define maxn  1<<3019 int dp[Size][11];20 /*表示  i個蘋果放在j個盤子裡面的所能放的種類21 dp[i][j]=dp[i][j-1]+dp[i-j][j]22 i個蘋果放在j個盤子裡面,23 只少有一個盤子為空白時, dp[i][j-1]24 沒有一個盤子為空白時 ,dp[i-j][j],與每個盤子都去掉一個蘋果的方法是一樣的25 如果盤子數大於蘋果數目那麼即為i>j26 dp[i][j]=dp[j][j]27 28 找出口的方法!!就是找出最初開始填的元素進行推導29 首先30 dp(1,1)=dp(1,0)+dp(0,1)  //dp(1,1) 是我們填表要填的第一個元素 其值為131 dp(2,1)=dp(2,0)+dp(1,1)  //其值為1   很明顯出口是m==0  和n==0   節上面兩個方程可以知道  dp(i,0)=0  dp(0,j)=1;32 dp(2,2)=dp(2,0)+dp(1,1)33 */34 int solve(int n, int m){35     if (dp[n][m] != -1) return dp[n][m];36     if (n == 0) {//看出口,一條路是n逐漸減小,  最終可能減到0 37         dp[n][m] = 1;38         return 1;39     }40     if (m == 0) {41         dp[n][m] = 0;42         return 0;43     }44     if (m > n) {45         dp[n][m]= solve(n, n);46         return dp[n][m];47     }48 49     dp[n][m]=solve(n - m, m) + solve(n, m - 1);//能走到這肯定是n-m>=1,而出口條件 dp[1][m]因為題目給的條件有水果數目為1的情況,不需要我們定義,其值為150     return dp[n][m];51 52 }53 int main(){54 55     int T;56     cin >> T;57     while (T--){58         int n, m;59         cin >> n >> m;60         for (int i = 0; i <= n; i++)61             for (int j = 0; j <= m; j++)62                 dp[i][j] = -1;63         cout << solve(n, m) << endl;64     }65     system("pause");66     return 0;67 }

 

動態規劃_百鍊1664 放蘋果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.