Mother's Milk

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值