zoukankan      html  css  js  c++  java
  • HDU

    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;
    }
    
  • 相关阅读:
    metasploitable实践
    免杀技术
    SQL注入
    ARP欺骗
    使用Nmap攻击靶机和使用Wireshark进行嗅探、分析
    Nmap
    搭建网络攻防环境
    20139216网络攻防技术第七次作业
    Android代码实现求和运算
    命令行输入参数,并实现排序
  • 原文地址:https://www.cnblogs.com/lifehappy/p/12604472.html
Copyright © 2011-2022 走看看