mother's milk AC

总算用上学到的数据结构了,广搜AC,这做到1.5虽然有点</font>慢,但不能放弃啊。</font></b></font>

Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours milk from one bucket to another until the second bucket is filled or the first bucket is empty. Once begun, a pour must be completed, of course. Being thrifty, no milk may be tossed out. </p>

Write a program to help FJ determine what amounts of milk he can leave in bucket C when he begins with three buckets as above, pours milk among the buckets for a while, and then notes that bucket A is empty. </p>

PROGRAM NAME: milk3</h3>

INPUT FORMAT</h3>

A single line with the three integers A, B, and C. </p>

SAMPLE INPUT (file milk3.in) </h3>
8 9 10 </pre>

OUTPUT FORMAT</h3>

A single line with a sorted list of all the possible amounts of milk that can be in bucket C when bucket A is empty. </p>

SAMPLE OUTPUT (file milk3.out)</h3>
1 2 8 9 10 </pre>

SAMPLE INPUT (file milk3.in) </h3>
2 5 10 </pre>

SAMPLE OUTPUT (file milk3.out)</h3>
5 6 7 8 9 10 

/*
ID: hippo he
LANG: C++
TASK: milk3
*/
#include
#include
#include
#include
#include
using namespace std;
typedef struct Tripple{
short int container[3];
}Tripple;
short int Max[3];
void input()
{
ifstream in("milk3.in");
in>>Max[0]>>Max[1]>>Max[2];
in.close();
}
bool arriGoal(Tripple t){
if(t.container[0]==0)return true;
return false;
}
bool exchanged(Tripple &u,int i,int j){
if(i==j||u.container[i]==0||u.container[j]==Max[j])return false;
if(u.container[i]-(Max[j]-u.container[j])>0){
u.container[i]-=Max[j]-u.container[j];
u.container[j]=Max[j];
}
else{
u.container[j]+=u.container[i];
u.container[i]=0;
}
return true;
}
void dfs(bool (*visited)[20][20],vector & res){
queue tr;
Tripple init;
init.container[0]=0;
init.container[1]=0;
init.container[2]=Max[2];
tr.push(init);
if(arriGoal(init))res.push_back(init.container[2]);
visited[0][0][Max[2]]=1;

while(!tr.empty()){
Tripple tmp=tr.front();
Tripple save=tmp;
tr.pop();

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if(exchanged(tmp,i,j)&&!visited[tmp.container[0]][tmp.container[1]][tmp.container[2]]){
tr.push(tmp);
visited[tmp.container[0]][tmp.container[1]][tmp.container[2]]=1;
if(arriGoal(tmp))res.push_back(tmp.container[2]);
}
tmp=save;
}
}
}
}
void output(vector &res)
{
ofstream file("milk3.out");
vector::iterator it=res.begin();
for (it = res.begin(); it!=res.end();it++)
if(it==res.begin())file<<*it;
else file<<" "<<*it;

file< file.close();
}
int main(int argc, char const* argv[])
{
bool visited[20][20][20];
vector res;
input();
dfs(visited,res);
sort(res.begin(),res.end());
res.erase(unique(res.begin(),res.end()),res.end());
output(res);
return 0;
}

</pre></div>