Advertisement
AlejandroGY

Untitled

May 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <cstdio>
  2. #include <array>
  3. #include <set>
  4. #include <vector>
  5. #include <utility>
  6. #include <algorithm>
  7.  
  8. using vasos = std::array<int, 3>;
  9.  
  10. int main( ) {
  11.     int v, w, n;
  12.     std::scanf("%d%d%d", &v, &w, &n);
  13.  
  14.     int pos[n + 2];
  15.     for (int i = 1; i <= n; ++i) {
  16.         std::scanf("%d", &pos[i]);
  17.     }
  18.     pos[0] = 0;
  19.     pos[n + 1] = v;
  20.  
  21.     std::set<vasos> memo;
  22.     std::vector<std::pair<vasos, int>> cola;
  23.     int ini = 0, fin = 1;
  24.  
  25.     cola.push_back({ { v, 0, 0 }, -1 });
  26.  
  27.     for (int d = 0; ini != fin; ++d) {
  28.         for (int f = fin; ini != f; ++ini) {
  29.             auto actual = cola[ini];
  30.             if (actual.first[0] == w || actual.first[1] == w || actual.first[2] == w) {
  31.                 std::printf("%d\n", d);
  32.  
  33.                 auto temp = actual;
  34.                 std::vector<vasos> solucion;
  35.  
  36.                 while (temp.second != -1) {
  37.                     vasos ti = temp.first;
  38.                     solucion.push_back(ti);
  39.                     temp = cola[temp.second];
  40.                 }
  41.  
  42.                 std::reverse(solucion.begin( ), solucion.end( ));
  43.  
  44.                 vasos ant = { v, 0, 0 }, act, sol;
  45.                 for (int i = 0; i < d; ++i, std::swap(ant, act)) {
  46.                     act = solucion[i];
  47.                     for (int j = 0; j < 3; ++j) {
  48.                         if (ant[j] != act[j]) {
  49.                             sol[(ant[j] > act[j] ? 0 : 1)] = j;
  50.                             sol[2] = std::abs(ant[j] - act[j]);
  51.                         }
  52.                     }
  53.                     std::printf("%d %d %d\n", sol[0], sol[1], sol[2]);
  54.                 }
  55.  
  56.                 return 0;
  57.             }
  58.  
  59.             for (int k = 0; k < 3; ++k) {
  60.                 for (int it : pos) {
  61.                     int tope[2] = { (k + 1) % 3, (k + 2) % 3 };
  62.                     for (int q = 0; q < 2; ++q) {
  63.                         vasos temp = actual.first;
  64.                         int x = it - temp[tope[q]];
  65.                         if (x >= 0 && x <= v - temp[tope[q]] && temp[k] >= x) {
  66.                             temp[k] -= x;
  67.                             temp[tope[q]] += x;
  68.                             auto itr = memo.insert(temp);
  69.                             if (itr.second) {
  70.                                 cola.push_back({ temp, ini }), ++fin;
  71.                             }
  72.                         }
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.     }
  78.     std::printf("-1\n");
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement