计蒜客————无脑博士的试管们

  •  17.62%
  •  1000ms
  •  65536K


无脑博士有三个容量分别是 A,B,CA,B,C 升的试管,A,B,CA,B,C 分别是三个从 11 到 2020 的整数,最初,AA 和 BB 试管都是空的,而 CC 试管是装满硫酸铜溶液的。有时,无脑博士把硫酸铜溶液从一个试管倒到另一个试管中,直到被灌试管装满或原试管空了。当然每一次灌注都是完全的。由于无脑博士天天这么折腾,早已熟练,溶液在倒的过程中不会有丢失。

写一个程序去帮助无脑博士找出当 AA 试管是空的时候,CC 试管中硫酸铜溶液所剩量的所有可能性。

输入格式

输入包括一行,为空格分隔开的三个数,分别为整数 A,B,CA,B,C

输出格式

输出包括一行,升序地列出当 AA 试管是空的时候,CC 试管溶液所剩量的所有可能性。

样例输入
2 5 10
样例输出
5 6 7 8 9 10
 
  
dfs模拟所有情况,使用记忆化数组防止重复访问
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<cstring>
#include<stack>
using namespace std;
#define trans(x,y,z) (10000*(x)+100*(y)+(z))
void func1(int &a,int maxa,int &b) { //b倒a
    if(maxa>=a+b) {
        a=a+b;
        b=0;
        return ;
    }
    b=b-maxa+a;
    a=maxa;
}
int t[3],Pow[3]= {10000,100,1};
void retrans(int x) {
    for(int i=0; i<3; i++) {
        t[i]=x/Pow[i];
        x=x%Pow[i];
    }
}
int main() {
    int in[3],dir[12]= {0,1,0,2,1,2,1,0,2,0,2,1};
    while(cin>>in[0]>>in[1]>>in[2]) {
        bool com[202021];
        memset(com,false,sizeof com);
        stack<int>sta1;
        set<int>se1;
        se1.insert(in[2]);
        sta1.push(trans(0,0,in[2]));
        com[trans(0,0,in[2])]=true;
        while(!sta1.empty()) {
            retrans(sta1.top());
            sta1.pop();
            for(int i=0; i<6; i++) {
                int x[3]= {t[0],t[1],t[2]};
                func1(x[dir[2*i]],in[dir[2*i]],x[dir[2*i+1]]);
                int temp=trans(x[0],x[1],x[2]);
                if(!com[temp]) {
                    sta1.push(temp);
                    com[temp]=true;
                    if(!x[0])
                        se1.insert(x[2]);
                }
            }
        }
        for(set<int>::iterator it=se1.begin(); it!=se1.end(); it++)
            if(it==se1.begin()) cout<<(*it);
            else cout<<' '<<(*it);
        cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值