- 17.62%
- 1000ms
- 65536K
无脑博士有三个容量分别是 A,B,C 升的试管,A,B,C 分别是三个从 1 到 20 的整数,最初,A 和 B 试管都是空的,而 C 试管是装满硫酸铜溶液的。有时,无脑博士把硫酸铜溶液从一个试管倒到另一个试管中,直到被灌试管装满或原试管空了。当然每一次灌注都是完全的。由于无脑博士天天这么折腾,早已熟练,溶液在倒的过程中不会有丢失。
写一个程序去帮助无脑博士找出当 A 试管是空的时候,C 试管中硫酸铜溶液所剩量的所有可能性。
输入格式
输入包括一行,为空格分隔开的三个数,分别为整数 A,B,C。
输出格式
输出包括一行,升序地列出当 A 试管是空的时候,C 试管溶液所剩量的所有可能性。
样例输入
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; }