Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. int N, M;
  7. vector< vector <int> > kovanice;
  8. vector <int> B, C, S, Z, P;
  9.  
  10. int memo[1024][5];
  11.  
  12. int rek(int sada, int zadnja) {
  13.   if (sada < 0) {
  14.     return 1000;
  15.   }
  16.   if (sada == 0) {
  17.     return 0;
  18.   }
  19.  
  20.   if (memo[sada][zadnja] != -1) {
  21.     return memo[sada][zadnja];
  22.   }  
  23.  
  24.   int min = 1000;
  25.  
  26.   for (int boja = 0; boja < 5; boja++) {
  27.     if (boja == zadnja) {
  28.       continue;
  29.     }
  30.     for (int i = 0; i < kovanice[boja].size(); i++) {
  31.       int mini = rek(sada - kovanice[boja][i], boja) + 1;
  32.       if (mini < min) {
  33.         min = mini;
  34.       }
  35.     }
  36.   }
  37.  
  38.   memo[sada][zadnja] = min;
  39.   return min;
  40. }
  41.  
  42. int main() {
  43.   cin >> N >> M;
  44.  
  45.   for (int i = 0; i < N; i++) {
  46.     int x; char b;
  47.     scanf("%d %c", &x, &b);
  48.  
  49.     if (b == 'B') {
  50.       B.push_back(x);
  51.     } else if (b == 'C') {
  52.       C.push_back(x);
  53.     } else if (b == 'S') {
  54.       S.push_back(x);
  55.     } else if (b == 'Z') {
  56.       Z.push_back(x);
  57.     } else if (b == 'P') {
  58.       P.push_back(x);
  59.     }
  60.   }
  61.  
  62.   kovanice.push_back(B);
  63.   kovanice.push_back(C);
  64.   kovanice.push_back(S);
  65.   kovanice.push_back(Z);
  66.   kovanice.push_back(P);
  67.  
  68.   memset(memo, -1, sizeof(memo));
  69.   cout << rek(M, 5) << endl;
  70.  
  71.   return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement