Advertisement
pexea12

Minh DSAP

Sep 11th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. void solve(int cup1, int cup2, int cup3, int target) {
  7.  
  8.       if (cup1 == target || cup2 == target || cup3 == target) {
  9.             cout << endl << "Done!";
  10.             return;
  11.       }
  12.       if (cup1 == 8) {
  13.             cup1 -= 3;
  14.             cup2 = 3;
  15.             cout << endl << "A -> B";
  16.             solve(cup1, cup2, cup3, target);
  17.       } else if (cup2 == 3) {
  18.             int remain3 = 5 - cup3;
  19.             if (remain3 < cup2) {
  20.                   cup2 -= remain3;
  21.                   cup3 = 5;
  22.                   cout << endl << "B -> C";
  23.                   solve(cup1, cup2, cup3, target);
  24.             } else {
  25.                   cup3 += cup2;
  26.                   cup2 = 0;
  27.                   cout << endl << "B -> C";
  28.                   solve(cup1, cup2, cup3, target);
  29.             }
  30.       } else if (cup3 == 5) {
  31.             cup1 += cup3;
  32.             cup3 = 0;
  33.             cout << endl << "C -> A";
  34.             solve(cup1, cup2, cup3, target);
  35.       } else {
  36.             if (cup2 == 0) {
  37.                   cup1 -= 3;
  38.                   cup2 = 3;
  39.                   cout << endl << "A -> B";
  40.                   solve(cup1, cup2, cup3, target);
  41.             } else if (cup3 == 0) {
  42.                   cup3 += cup2;
  43.                   cup2 = 0;
  44.                   cout << endl << "B -> C";
  45.                   solve(cup1, cup2, cup3, target);
  46.             } else {
  47.                   int remain3 = 5 - cup3;
  48.                   if (remain3 < cup2) {
  49.                         cup2 -= remain3;
  50.                         cup3 = 5;
  51.                         cout << endl << "B -> C";
  52.                         solve(cup1, cup2, cup3, target);
  53.                   } else {
  54.                         cup2 = 0;
  55.                         cup3 += cup2;
  56.                         cout << endl << "B -> C";
  57.                         solve(cup1, cup2, cup3, target);
  58.                   }
  59.             }
  60.       }
  61.  
  62.      
  63.  }
  64.  
  65. int main()
  66. {
  67.       int n;
  68.       cout << "n = "; cin >> n;
  69.  
  70.       solve(8, 0, 0, n);
  71.  
  72.       return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement