Advertisement
tepyotin2

Pouring Wine

May 8th, 2025
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Cup{
  6.     int first, second, third, count;
  7.     bool operator<(const Cup& a) const{
  8.         if(first != a.first){
  9.             return first<a.first;
  10.         }else if(second != a.second){
  11.             return second<a.second;
  12.         }
  13.         return third<a.third;
  14.     }
  15. };
  16.  
  17. //Cup start;
  18. queue<Cup> q;
  19. set<Cup> visited;
  20. int start[3];
  21.  
  22. int main(){
  23.     //freopen("wine.in", "r", stdin);
  24.    
  25.     cin >> start[0] >> start[1] >> start[2];
  26.     visited.insert({start[0], 0, 0, 0});
  27.     //visited.insert({start.first, 0, 0, 1});
  28.     q.push({start[0], 0, 0, 0});
  29.     while(!q.empty()){
  30.         Cup cur = q.front();
  31.         q.pop();
  32.         //cout << cur.first << ", " << cur.second << ", " << cur.third << '\n';
  33.         //cout << cur.count << '\n';
  34.         if((cur.first==cur.second && cur.third == 0) || (cur.first == cur.third && cur.second == 0) || (cur.second == cur.third && cur.first == 0)){
  35.             //cout << "HI" << '\n';
  36.             //cout << cur.first << ", " << cur.second << ", " << cur.third << '\n';
  37.             cout << cur.count << '\n';
  38.             return 0;
  39.         }
  40.         for(int i=0; i<3; i++){
  41.             for(int j=0; j<3; j++){
  42.                 if(i==j){
  43.                     continue;
  44.                 }
  45.                 //cout << "i: " << i << ", j: " << j << '\n';
  46.                 int val[3] = {cur.first, cur.second, cur.third};
  47.                 if(val[i]>0){
  48.                     //cout << "i: " << i << ", j: " << j << '\n';
  49.                     if(val[i]+val[j]>start[j]){
  50.                         val[i] = (val[i]+val[j])-start[j];
  51.                         val[j] = start[j];
  52.                     }else if(val[i]+val[j]<=start[j]){
  53.                         val[j] = val[i]+val[j];
  54.                         val[i] = 0;
  55.                     }
  56.                     Cup temp = {val[0], val[1], val[2], cur.count+1};
  57.                     if(visited.find(temp) == visited.end()){
  58.                         //cout << "1: " << val[0] << ", 2: " << val[1] << ", 3: " << val[2] << '\n';
  59.                         visited.insert(temp);
  60.                         q.push(temp);
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.     }
  66.     cout << "NO" << '\n';
  67.    
  68.     return 0;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement