zoj 1005 郁闷~~
这题我一开始想的是用广搜做,用广搜可以过,记录搜索路径要开个二位数组,比较麻烦,效率也不高。其实这题没必要广搜,题目是Special Judge,就是OJ有一个特殊判断程序,答案不是唯一的,只要结果对就行。很容易得出这个结论:只要给的两个桶的容量互质,那么从0到大桶的容量之间的任何一个水的体积都能得到,而且用一种固定的方法就能遍历每个体积。
遍历方法为:
循环开始
if(小桶空) then 倒满小桶 (fill 小桶)
将小桶的水倒入大桶,直到小桶空或大桶满 (pour 小桶 大通)
if(大桶的水 == 所求的体积) then 跳出循环 (success)
if(大桶满了) then 清空大桶 (empty 大桶)
#include <iostream> using namespace std; int main() { int first,second,end; while(cin>>first>>second>>end) { int Ca=0,Cb=0; for(;;) { if(Ca==0){ Ca=first;cout<<"fill A"<<endl;} if(Ca+Cb>second) { Ca=Ca+Cb-second; Cb=second; cout<<"pour A B"<<endl; } else{ Cb=Ca+Cb; Ca=0; cout<<"pour A B"<<endl; } if(Cb==end) {cout<<"success"<<endl; break;} if(Cb==second) {Cb=0;cout<<"empty B"<<endl;} } } return 0; }
然后自己的代码,很垃圾,而且是不对,谁找出错误的话帮一下吧,是广度优先~
#include<iostream> using namespace std; int first,second,end; bool find(int S1,int S2) { if(S1==end||S2==end) return true; else if(S1>0&&S2>0) { if(S1<first&&S2<second){ if(find(first,S2)) {cout<<"fill A"<<endl; return true;} else if(find(0,second)) {cout<<"empty A"<<endl; return true;} else if(find(S1,second)) {cout<<"fill B"<<endl; return true;} else if(find(first,0)) {cout<<"empty B"<<endl;return true;} else if(S1>=S2) //S1,daoruS2 { if(second-S2>S1) { if(find(0,S1+S2)) { cout<<"pour A B"<<endl;return true; } //return false; } else if(find(S1+S2-second,second)) {cout<<"pour A B"<<endl;return true;} //return false; } else if(S2>=S1) { if(first-S1>S2){ if(find(S1+S2,0)) { cout<<"pour B A"<<endl; return true; } //return false; } else if(find(first,S1+S2-first)) {cout<<"pour B A"<<endl;return true;} //return false; } } else if(S1<first&&S2==second){ /* if(second>first)*/ { if(find(first,second-first)) {cout<<"pour B A"<<endl;return true;} //return false; } /*else if(find(second,0)) {cout<<"pour B A"<<endl; return true;}*/ //return false; } else if(S1==first&&S2<second) { // if(first>second) //{ // if(find(first-second,second)){ cout<<"pour A B"<<endl; return true;} // //return false; //} /* else*/ if(find(0,first)) {cout<<"pour B A"<<endl; return true;} //return false; } else if(S1==first&&S2==second) { if(find(0,second)) {cout<<"empty A"<<endl; return true;} else if(find(first,0)) {cout<<"empty B"<<endl;return true;} //return false; } } else if(S1>0&&S2==0) { if(S1<first) { if(find(first,0)){ cout<<"fill A"<<endl; return true;} //else if(S1>=second) //{ // if(find(S1-second,second)) {cout<<"pour A B"<<endl; return true;} // //return false; //} else if(find(0,S1)) {cout<<"pour A B"<<endl; return true;} //return false; } //else if(S1>=second) // { // if(find(S1-second,second)) {cout<<"pour A B"<<endl; return true;} // //return false; // } else if(find(0,S1)) {cout<<"pour A B"<<endl; return true;} //return false; } else if(S1==0&&S2>0) { if(S2<second){ if(find(0,second)) {cout<<"fill B"<<endl; return true;} else if(S2>=first) { if(find(first,S2-first)) {cout<<"pour B A"<<endl; return true;} //return false; } else if(find(S2,0)) {cout<<"pour B A"<<endl; return true;} //return false; } else if(S2>=first) { if(find(first,S2-first)) {cout<<"pour B A"<<endl; return true;} //return false; } else if(find(S2,0)){ cout<<"pour B A"<<endl; return true;} //return false; } else if(S1==0&&S2==0) { if(find(first,0)){cout<<"fill A"<<endl; return true; } else if(find(0,second)){cout<<"fill B"<<endl;return true;} } return false; } int main() { while(cin>>first>>second>>end) { if(find(0,0)) cout<<"success"<<endl; cout<<"Stop!"<<endl; } return 0; }