1005 Jugs-zju
来源:互联网 发布:怎么在mac上下载webex 编辑:程序博客网 时间:2024/06/18 11:09
也是一道DFS的题目,但是要注意保存搜过的状态,不然会死循环的!!!
#include<iostream> #include<string>#include<stack>#include<vector>using namespace std; vector<char> v;//a->b :'A' , b->a:'B' , fill a: 'C' , fill b: 'D' , empty a:'E', empty b:'F' vector<int> reca,recb;int A,B,t;void output(){for(int i=0;i<v.size();i++){if(v[i]=='A')cout<<"pour A B"<<endl;else if(v[i]=='B')cout<<"pour B A"<<endl;else if(v[i]=='C')cout<<"fill A"<<endl;else if(v[i]=='D')cout<<"fill B"<<endl;else if(v[i]=='E')cout<<"empty A"<<endl;else if(v[i]=='F')cout<<"empty B"<<endl;}cout<<"success"<<endl;}bool flag;//a->b :'A' , b->a:'B' , fill a: 'C' , fill b: 'D' , empty a:'E', empty b:'F' inline bool check(int a,int b){for(int i=0;i<reca.size();i++){if(reca[i]==a && recb[i]==b){return false;}}return true;}void solve(int tempa,int tempb){if(flag==true)return;if(tempa==t || tempb==t){output();flag=true;return;}//fill a: 'C' if(tempa!=A && (v.empty()==true || v[v.size()-1]!='E')){if(check(A,tempb)){reca.push_back(A);recb.push_back(tempb);v.push_back('C');solve(A,tempb);v.pop_back();reca.pop_back();recb.pop_back();}}if(tempb!=B && (v.empty()==true || v[v.size()-1]!='F')){// fill b: 'D'if(check(tempa,B)){reca.push_back(tempa);recb.push_back(B);v.push_back('D');solve(tempa,B);v.pop_back();reca.pop_back();recb.pop_back();}}if(tempa){int ss,tt;//a->b :'A'int restb=B-tempb;if(restb>=tempa){ss=0;tt=tempb+tempa;}else{ss=tempa-restb;tt=B;}if(check(ss,tt)){reca.push_back(ss);recb.push_back(tt);v.push_back('A');solve(ss,tt);v.pop_back();reca.pop_back();recb.pop_back();}}if(tempb && (tempb+tempa)<=A){//b->a :'B'int ss,tt;int resta=A-tempa;if(resta>=tempb){ss=tempb+tempa;tt=0;}else{ss=A;tt=tempb-resta;}if(check(ss,tt)){v.push_back('B');reca.push_back(ss);recb.push_back(tt);solve(ss,tt);v.pop_back();reca.pop_back();recb.pop_back();}}if(tempa!=0 && (v.empty()==true || v[v.size()-1]!='C')){// empty a:'E', empty b:'F' if(check(0,tempb)){reca.push_back(0);recb.push_back(tempb);v.push_back('E');solve(0,tempb);v.pop_back();reca.pop_back();recb.pop_back();}}if(tempb!=0 && (v.empty()==true || v[v.size()-1]!='D')){if(check(tempa,0)){reca.push_back(tempa);recb.push_back(0);v.push_back('F');solve(tempa,0);v.pop_back();reca.pop_back();recb.pop_back();}}}int main(){while(cin>>A>>B>>t){reca.clear();recb.clear();flag=false;solve(0,0);}return 0;}
- 1005 Jugs-zju
- 1005 Jugs
- 1005 Jugs
- ZOJ 1005 - Jugs | UVA 571 - Jugs
- ZOJ-1005-Jugs
- ZOJ 1005 Jugs
- ZOJ 1005 Jugs
- zoj 1005 Jugs
- ZOJ 1005 Jugs (DFS)
- zoj 1005 Jugs
- zoj 1005 Jugs
- ZOJ 1005 Jugs 【BFS】
- ZOJ 1005 Jugs
- zoj 1005 jugs
- ZOJ 1005 Jugs
- zoj 1005Jugs
- zoj 1005 Jugs (模拟)
- 模拟 zoj 1005 Jugs
- 正则表达式 .
- Servlet初始化相关问题,以及Spring容器初始化
- 1008-Gnome Tetravex -zju
- 使用Runtime.getRuntime().exec()的方法调用本地mysqldump.exe备份还原数据库(路径中含有空格和数字)的方法
- IPv6地址表达方式、寻址模型和地址空间
- 1005 Jugs-zju
- SEO优化(1)
- Anagrams by Stack--zju
- PHP+FLASH实现上传文件进度条
- Sql Server中Convert函数小结
- Shell编程简单实现四则运算
- 测量网络的MTU值
- 图片处理收集
- hash函数的学习及应用实例