USACO 1.4 Mother's Milk

记忆化搜索即可……f[i][j][k]表示这个状态是否到达过……


/*
TASK:milk3
LANG:C++
*/
#include <iostream>
#include <cstdio>
using namespace std;

bool f[22][22][22] = {0};
bool ans[22] = {0};
int A, B, C;

inline void change(int &a,int &b, int B) // a->b
{
	b = a + b;	
	a = 0;
	if (b > B)
	{
		a = b - B;
		b = B;	
	}
}

void dfs(int a, int b, int c)
{
	f[a][b][c] = true;
	if (!a)	ans[c] = true;
	int x = a, y = b, z = c;	
	change(x, y, B);
	if (!f[x][y][z])	dfs(x, y, z);
	x = a, y = b, z = c;
	change(x, z, C);
	if (!f[x][y][z])	dfs(x, y, z);
	x = a, y = b, z = c;
	change(y, x, A);
	if (!f[x][y][z])	dfs(x, y, z);
	x = a, y = b, z = c;
	change(y, z, C);
	if (!f[x][y][z])	dfs(x, y, z);
	x = a, y = b, z = c;
	change(z, x, A);
	if (!f[x][y][z])	dfs(x, y, z);
	x = a, y = b, z = c;
	change(z, y, B);
	if (!f[x][y][z])	dfs(x, y, z);
}


int main()
{
	freopen("milk3.in", "r", stdin);
	freopen("milk3.out","w", stdout);
	cin >> A >> B >> C;
	dfs(0, 0, C);
	int outputbuff[22], t= 0;
	for (int i = 0; i != 22; ++ i)
		if (ans[i])	outputbuff[++t] = i;
	for (int i = 1; i < t; ++ i)	cout<<outputbuff[i]<<" ";
	cout<<outputbuff[t]<<endl;
	return 0;
}



你可能感兴趣的:(USACO 1.4 Mother's Milk)