Pots

Problem Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

 

FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;

DROP(i)      empty the pot i to the drain;

POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input

There are multiple test cases.

 On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output

 The first line of the output must contain the length of the sequence of operations K.  If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Example Input
3 5 4
Example Output
6
Hint

FILL(2)

POUR(2,1)

DROP(1)

POUR(2,1)

FILL(2)

POUR(2,1)

Author
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e2 + 1;
int graph[maxn][maxn];
bool visited[maxn][maxn];
struct node {
    int a, b, step;
};
/*题意
有两个容量A,B一定的罐子,分别有装满,倒空,从一个瓶子倒入另一个瓶子这三种操作,求至少经过多少次操作后会使得其中一个瓶子中的水的体积为C

思路
因为求最少,可以用bfs模拟倒水
应该很容易就能分析出,对应于每一种状态的下一步只有六种情况:
一:用水池中的水装满 A
二:用水池中的水装满 B
三:把 A 中的水全部倒掉
四:把 B 中的水全部倒掉
五:把 A 中的水倒进 B, 但是主意不能溢出。
    那么对应可能会有两种状态:用 f.x 表示 A 中的水, f.b 表示 B 中的水
    如果 f.x+f.y >= B 则   f.x = f.x+f.y-B; f.y= B 【能够装满容器 B】
    否则 k1 = k1+k2; k2 = 0 【不能能够装满容器 B】
六:把 B 中的水倒进 A 【不能溢出】
    也有两种情况,分析同上;

*/
void BFS(int a, int b, int c) {
    node p, q;
    p.a = p.b = p.step = 0;
    visited[p.a][p.b] = 1;
    queue<node> Q;
    Q.push(p);
    while(!Q.empty()) {
        p = Q.front();
        Q.pop();
        if(p.a == c || p.b == c) {
            printf("%d\n", p.step);
            return;
        }
        for(int i = 0; i < 6; i++) {
            if(i == 0) {
                q.a = a;
                q.b = p.b;
            } else if(i == 1) {
                q.a = p.a;
                q.b = b ;
            } else if(i == 2) {
                q.a = 0;
                q.b = p.b;
            } else if(i == 3) {
                q.a = p.a;
                q.b = 0;
            } else if(i == 4) {
                q.a = p.a + p.b;
                if(q.a > a) {
                    q.b = q.a - a;
                    q.a = a;
                } else {
                    q.b = 0;
                }
            } else {
                q.b = p.a + p.b;
                if(q.b > b) {
                    q.a = q.b - b;
                    q.b = b;
                } else {
                    q.a = 0;
                }
            }
            if(!visited[q.a][q.b]) {
                q.step = p.step + 1;
                Q.push(q);
                visited[q.a][q.b] = 1;
            }
        }
    }
    printf("impossible\n");
}
int main() {
    int a, b, c;
    while(~scanf("%d%d%d", &a, &b, &c)) {
        memset(visited, 0, sizeof(visited));
        BFS(a, b, c);
    }
    return 0;
}


/***************************************************
User name: 
Result: Accepted
Take time: 0ms
Take Memory: 168KB
Submit time: 2018-01-30 14:16:07
****************************************************/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值