HDU - 1495 非常可乐

HDU - 1495 题目链接
又是最小操作次数,那么铁定bfs无疑了,因此我们只要确定操作就行了,有如下六种操作
一、S倒入N。
二、S倒入M。
三、N倒入S。
四、N倒入M。
五、M倒入S。
六、M倒入N。
接下来就是进行bfs操作了

//Powered by CK
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int N = 110;
int s, n, m, visit[N][N][N];
struct state {
    int s, n, m, step;
    state(int a, int b, int c, int d) : s(a), n(b), m(c), step(d) {}
};
int main() {
    ios::sync_with_stdio(false);
    // freopen("in.txt", "r", stdin);
    while(cin >> s >> n >> m && s) {
        if(s % 2) {
            cout << "NO" << endl;
            continue;
        }
        memset(visit, 0, sizeof visit);
        queue<state> q;
        visit[s][n][m] = 1;
        int ans = 0;
        int flag = 0;
        q.push(state(s, 0, 0, 0));
        while(!q.empty()) {
            state temp = q.front();
            q.pop();
            // cout << temp.s << " " << temp.n << " " << temp.m << endl;
            flag = 0;
            if(temp.s == s / 2) flag++;
            if(temp.n == s / 2) flag++;
            if(temp.m == s / 2) flag ++;
            if(flag == 2) {
                cout << temp.step << endl;
                ans = 1;
                break;
            }
            if(temp.s) {
                if(temp.n != n) {//操作一
                    int add = min(n - temp.n, temp.s);
                    if(!visit[temp.s - add][temp.n + add][temp.m]++)
                        q.push(state(temp.s - add, temp.n + add, temp.m, temp.step + 1));
                }
                if(temp.m != m) {//操作二
                    int add = min(m - temp.m, temp.s);
                    if(!visit[temp.s - add][temp.n][temp.m + add]++)
                        q.push(state(temp.s - add, temp.n, temp.m + add, temp.step + 1));
                }
            }
            if(temp.n) {
                if(temp.s != s) {//操作三
                    int add = min(s - temp.s, temp.n);
                    if(!visit[temp.s + add][temp.n - add][temp.m]++)
                        q.push(state(temp.s + add, temp.n -add, temp.m, temp.step + 1));
                }
                if(temp.m != m) {//操作四
                    int add = min(m - temp.m, temp.n);
                    if(!visit[temp.s][temp.n - add][temp.m + add]++)
                        q.push(state(temp.s, temp.n - add, temp.m + add, temp.step + 1));
                }
            }
            if(temp.m) {
                if(s != temp.s) {//操作五
                    int add = min(s - temp.s, temp.m);
                    if(!visit[temp.s + add][temp.n][temp.m - add]++)
                        q.push(state(temp.s + add, temp.n, temp.m - add, temp.step + 1));
                }
                if(n != temp.n) {//操作六
                    int add = min(n - temp.n, temp.m);
                    if(!visit[temp.s][temp.n + add][temp.m - add]++)
                        q.push(state(temp.s, temp.n + add, temp.m -add, temp.step + 1));
                }
            }
        }
        if(ans == 0)    cout << "NO" << endl;
    }
    return 0;
}
posted @ 2020-01-08 13:01  lifehappy  阅读(157)  评论(0编辑  收藏  举报