Advertisement
pdaogu

WATERJUG_BFS

Sep 29th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAX 901
  3. using namespace std;
  4.  
  5. int a, b, c;
  6. int result;
  7. deque<int> qx;      // queue for first jug
  8. deque<int> qy;      // queue for second jug
  9. enum Transition
  10. {
  11.     PourA, PourB, EmptyA, EmptyB, FillA, FillB
  12. };
  13. int visited[MAX][MAX];      // visited[i][j] = 1 -> cell (i, j) visited
  14. int level[MAX][MAX];        // level[i][j] shortest distance from state (0, 0) to (i, j)
  15.  
  16. void output () {
  17.     cout << result << endl;
  18. }
  19.  
  20. void dfs () {
  21.     Transition vv;
  22.     level[0][0] = 0;
  23.     int action[10];
  24.     while (!qx.empty()) {
  25.         int x = qx.front();
  26.         qx.pop_front();
  27.         int y = qy.front();
  28.         qy.pop_front();
  29.        
  30.         for (int i = 0; i < 6; ++i)
  31.             action[i] = 1;
  32.         if (x == 0) {
  33.             action[PourA] = action[EmptyA] = 0;
  34.         }
  35.         if (y == 0) {
  36.             action[PourB] = action[EmptyB] = 0;
  37.         }
  38.         if (x == a) {
  39.             action[FillA] = 0;
  40.         }
  41.         if (y == b) {
  42.             action[FillB] = 0;
  43.         }
  44.         for (int i = 0; i < 6; ++i) {
  45.             if (action[i]) {
  46.                 int tmpX;
  47.                 int tmpY;
  48.                 switch (i) {
  49.                     case PourA:
  50.                         if (y + x > b) {
  51.                             tmpX = x - (b - y);
  52.                             tmpY = b;
  53.                         } else {
  54.                             tmpX = 0;
  55.                             tmpY = y + x;
  56.                         }
  57.                         break;
  58.                     case PourB:
  59.                         if (x + y > a) {
  60.                             tmpX = a;
  61.                             tmpY = y - (a - x);
  62.                         } else {
  63.                             tmpX = x + y;
  64.                             tmpY = 0;
  65.                         }
  66.                         break;
  67.                     case EmptyA:
  68.                         tmpX = 0;
  69.                         tmpY = y;
  70.                         break;
  71.                     case EmptyB:
  72.                         tmpX = x;
  73.                         tmpY = 0;
  74.                         break;
  75.                     case FillA:
  76.                         tmpX = a;
  77.                         tmpY = y;
  78.                         break;
  79.                     case FillB:
  80.                         tmpX = x;
  81.                         tmpY = b;
  82.                         break;
  83.                 }
  84.                 if (!visited[tmpX][tmpY]) {
  85.                     qx.push_back(tmpX);
  86.                     qy.push_back(tmpY);
  87.                     level[tmpX][tmpY] = level[x][y] + 1;
  88.                     visited[tmpX][tmpY] = 1;
  89.                     // cout << tmpX << tmpY << level[tmpX][tmpY] << i << endl;
  90.                     if (tmpX == c || tmpY == c) {
  91.                         result = level[tmpX][tmpY];
  92.                         return;
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.     result = -1;
  100. }
  101.  
  102. void solve () {
  103.     qx.push_back(0);
  104.     qy.push_back(0);
  105.     visited[0][0] = 1;
  106.     dfs();
  107. }
  108.  
  109. void input () {
  110.     cin >> a >> b >> c;
  111. }
  112.  
  113. int main () {
  114.     //freopen("inp", "r", stdin);
  115.     //freopen("out", "w", stdout);
  116.     input();
  117.     solve();
  118.     output();
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement