Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define MAX 901
- using namespace std;
- int a, b, c;
- int result;
- deque<int> qx; // queue for first jug
- deque<int> qy; // queue for second jug
- enum Transition
- {
- PourA, PourB, EmptyA, EmptyB, FillA, FillB
- };
- int visited[MAX][MAX]; // visited[i][j] = 1 -> cell (i, j) visited
- int level[MAX][MAX]; // level[i][j] shortest distance from state (0, 0) to (i, j)
- void output () {
- cout << result << endl;
- }
- void dfs () {
- Transition vv;
- level[0][0] = 0;
- int action[10];
- while (!qx.empty()) {
- int x = qx.front();
- qx.pop_front();
- int y = qy.front();
- qy.pop_front();
- for (int i = 0; i < 6; ++i)
- action[i] = 1;
- if (x == 0) {
- action[PourA] = action[EmptyA] = 0;
- }
- if (y == 0) {
- action[PourB] = action[EmptyB] = 0;
- }
- if (x == a) {
- action[FillA] = 0;
- }
- if (y == b) {
- action[FillB] = 0;
- }
- for (int i = 0; i < 6; ++i) {
- if (action[i]) {
- int tmpX;
- int tmpY;
- switch (i) {
- case PourA:
- if (y + x > b) {
- tmpX = x - (b - y);
- tmpY = b;
- } else {
- tmpX = 0;
- tmpY = y + x;
- }
- break;
- case PourB:
- if (x + y > a) {
- tmpX = a;
- tmpY = y - (a - x);
- } else {
- tmpX = x + y;
- tmpY = 0;
- }
- break;
- case EmptyA:
- tmpX = 0;
- tmpY = y;
- break;
- case EmptyB:
- tmpX = x;
- tmpY = 0;
- break;
- case FillA:
- tmpX = a;
- tmpY = y;
- break;
- case FillB:
- tmpX = x;
- tmpY = b;
- break;
- }
- if (!visited[tmpX][tmpY]) {
- qx.push_back(tmpX);
- qy.push_back(tmpY);
- level[tmpX][tmpY] = level[x][y] + 1;
- visited[tmpX][tmpY] = 1;
- // cout << tmpX << tmpY << level[tmpX][tmpY] << i << endl;
- if (tmpX == c || tmpY == c) {
- result = level[tmpX][tmpY];
- return;
- }
- }
- }
- }
- }
- result = -1;
- }
- void solve () {
- qx.push_back(0);
- qy.push_back(0);
- visited[0][0] = 1;
- dfs();
- }
- void input () {
- cin >> a >> b >> c;
- }
- int main () {
- //freopen("inp", "r", stdin);
- //freopen("out", "w", stdout);
- input();
- solve();
- output();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement