洛谷 P1215 【[USACO1.4]母亲的牛奶 Mother's Milk】

这道题\(DFS\)就好了,六种情况,\(ab,ac,ba,bc,ca,cb\),我们直接枚举就可。什么?这样不会结束?用一个\(vis\)数组判断走过没有就可以了。最后排序输出即可。

\(code:\)

#include 
using namespace std;
int a , b , c , tot;
int ans[100010] , vis[35][35][35];
void dfs(int x , int y , int z){
	if(x == 0) ans[++tot] = z;
	if(x != 0){
		int xx = x , yy = y , zz = z;
		y += x;
		x = 0;
		if(y > b) x += y - b , y = b;
		if(!vis[x][y][z]){
			vis[x][y][z] = 1;
			dfs(x , y , z);
		}
		x = xx , y = yy , z = zz;
		z += x;
		x = 0;
		if(z > c) x += z - c , z = c;
		if(!vis[x][y][z]){
			vis[x][y][z] = 1;
			dfs(x , y , z);
		}
		x = xx , y = yy , z = zz;
	}
	if(y != 0){
		int xx = x , yy = y , zz = z;
		x += y;
		y = 0;
		if(x > a) y += x - a , x = a;
		if(!vis[x][y][z]){
			vis[x][y][z] = 1;
			dfs(x , y , z);
		}
		x = xx , y = yy , z = zz;
		z += y;
		y = 0;
		if(z > c) y += z - c , z = c;
		if(!vis[x][y][z]){
			vis[x][y][z] = 1;
			dfs(x , y , z);
		}
		x = xx , y = yy , z = zz;
	}
	if(z != 0){
		int xx = x , yy = y , zz = z;
		x += z;
		z = 0;
		if(x > a) z += x - a , x = a;
		if(!vis[x][y][z]){
			vis[x][y][z] = 1;
			dfs(x , y , z);
		}
		x = xx , y = yy , z = zz;
		y += z;
		z = 0;
		if(y > b) z += y - b , y = b;
		if(!vis[x][y][z]){
			vis[x][y][z] = 1;
			dfs(x , y , z);
		}
		x = xx , y = yy , z = zz;
	}
}
int main(){
	cin >> a >> b >> c;
	vis[0][0][c] = 1;
	dfs(0 , 0 , c);
	sort(ans + 1 , ans + tot + 1);
	for(int i = 1; i <= tot; i++) cout << ans[i] << " ";
	return 0;
}

你可能感兴趣的:(洛谷 P1215 【[USACO1.4]母亲的牛奶 Mother's Milk】)