倒水问题

#include<bits/stdc++.h>
using namespace std;
int v1, v2, v3, ans, a, b, z, x1, x2;
//map<node, int> m;
bool m[300][300];

void js(int i)
{
    if(i == 1)
        x1 = v1, x2 = b;
    else if(i == 2)
        x1 = a, x2 = v2;
    else if(i == 3)
        x1 = 0, x2 = b;
    else if(i == 4)
        x1 = a, x2 = 0;
    else if(i == 5)
        if(b > v1 - a) x1 = v1, x2 = b - (v1 - a);
        else x1 = a + b, x2 = 0;
    else if(i == 6)
        if(a > v2 - b) x2 = v2, x1 = a - (v2 - b);
        else x2 = a + b, x1 = 0;
}

int main()
{
    queue<int> q;
    scanf("%d%d%d", &v1, &v2, &v3);
    q.push(0), q.push(0), q.push(0);
    while(!q.empty()){
        a = q.front();
        q.pop();
        b = q.front();
        q.pop();
        z = q.front();
        q.pop();
        if(a == v3 || b == v3 || a + b == v3){
            ans = z;
            break;
        }
        for(int i = 1; i <= 6; i++){
            js(i);
            if(!m[x1][x2]){
                m[x1][x2] = 1;
                q.push(x1), q.push(x2), q.push(z +1);
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

 

posted @ 2018-07-18 16:19  wbss  阅读(192)  评论(0编辑  收藏  举报