http://train.usaco.org/usacoprob2?a=EN6XVH05xAs&S=milk3
题目大意:
A,B,C三个容器,每个容器都有固定的体积,刚开始A,B是空的,C装满牛奶;三个容器中的牛奶可以相互倒,每次倒,直到一个容器倒空或另一个容器倒满为止;找出当A为空是,C容器中可能剩下的牛奶;
#include <iostream>
#include <fstream>
#include <algorithm>
//倒水的策略:
using namespace std;
struct bucket
{
int capacity;
int state;
} a[3];
bool ans[22], f[22][22][22]; //f[a][b][c]:判断这种状态是否搜索过
void pour(bucket& to, bucket& from)
{
if(to.capacity - to.state > from.state) to.state += from.state, from.state = 0;
else from.state = from.state - (to.capacity - to.state), to.state = to.capacity;
}
void dfs()
{
if(f[a[0].state][a[1].state][a[2].state]) return;
if(a[0].state == 0)
if(!ans[a[2].state]) {ans[a[2].state] = true;}
f[a[0].state][a[1].state][a[2].state] = true;
bucket t0 = a[0], t1 = a[1], t2 = a[2];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(i == j) continue;
pour(a[j], a[i]);
dfs();
a[0] = t0;
a[1] = t1;
a[2] = t2;
}
}
}
int main()
{
ifstream fin("milk3.in");
ofstream fout("milk3.out");
fin >> a[0].capacity >> a[1].capacity >> a[2].capacity;
a[0].state = a[1].state = 0;
a[2].state = a[2].capacity;
dfs();
for(int i = 0; i <= a[2].capacity; i++)
{
if(i > 0 && ans[i - 1]) fout <<" ";
if(ans[i]) fout << i;
}
fout << endl;
fout.close();
return 0;
}