sgf87

zoj 1005 郁闷~~

遍历方法为：
循环开始
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;
}



posted on 2010-04-28 20:19  sgf87  阅读(...)  评论(...编辑  收藏