非常可乐 HDU - 1495

这道题题意不是很清楚啊,没说平分的含义有木有啊!
首先,平分是指可乐瓶中的可乐含量是原来的一半,可以首先把奇数的样例pass掉了。然后用BFS模拟即可。

// HDU - 1495
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

struct zt {
    int v[3];
    int n[3];
    int step;
};

int dir[][2] = {{0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 0}, {2, 1}};
bool vis[110][110];

int main() {
    freopen("input.txt", "r", stdin);
    int S, N, M;
    while(scanf("%d%d%d", &S, &N, &M) && S) {
        zt b;
        if (S%2 == 1) { printf("NO\n"); continue;}
        int aim = S/2;
        b.step = 0;
        b.v[0] = S;
        b.v[1] = N;
        b.v[2] = M;
        b.n[0] = S;
        b.n[1] = b.n[2] = 0;
        queue<zt> qu;
        qu.push(b);
        bool flag = true;
        memset(vis, false, sizeof(vis));
        vis[b.n[1]][b.n[2]] = true;
        while(!qu.empty()) {
            zt v = qu.front(); qu.pop();
            if (v.n[0] == aim) {
                printf("%d\n", v.step);
                flag = false;
                break;
            }
            for(int i = 0; i < 6; i++) {
                zt t = v;
                t.step++;
                int fr = dir[i][0];
                int to = dir[i][1];
                if (t.n[fr] > t.v[to] - t.n[to]) {
                    t.n[fr] = t.n[fr] - (t.v[to] - t.n[to]);
                    t.n[to] = t.v[to];
                }
                else {
                    t.n[to] += t.n[fr];
                    t.n[fr] = 0;
                }
                if (!vis[t.n[1]][t.n[2]])  { qu.push(t);  vis[t.n[1]][t.n[2]] = true;}
            }
        }
        if (flag) printf("NO\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sunmaoxiang/article/details/80867323