usaco1.4.2 Mother's Milk

本文探讨了一个经典的倒水问题,即如何通过有限次数的倒水操作,在三个不同容量的桶中实现特定水量的分配。特别地,分析了初始状态下只有一个桶满水的情况,并通过深度优先搜索算法(DFS)寻找所有可能的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 原题

Mother's Milk

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.

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.

PROGRAM NAME: milk3

INPUT FORMAT

A single line with the three integers A, B, and C.

SAMPLE INPUT (file milk3.in)

8 9 10

OUTPUT FORMAT

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.

SAMPLE OUTPUT (file milk3.out)

1 2 8 9 10

SAMPLE INPUT (file milk3.in)

2 5 10

SAMPLE OUTPUT (file milk3.out)

5 6 7 8 9 10

二. 分析

有3个给定容量(a,b,c<=20)的桶,初始时只有3号桶为满,1,2号桶为空。一次倒水只能把桶倒空或者把目标桶倒满。问1号桶为空时,3号桶里可能的残余的水的体积。搜索空间很小,总共C(21, 2)个状态,dfs即可。

三. 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: milk3
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4188 KB]
   Test 2: TEST OK [0.000 secs, 4188 KB]
   Test 3: TEST OK [0.000 secs, 4188 KB]
   Test 4: TEST OK [0.000 secs, 4188 KB]
   Test 5: TEST OK [0.000 secs, 4188 KB]
   Test 6: TEST OK [0.000 secs, 4188 KB]
   Test 7: TEST OK [0.000 secs, 4188 KB]
   Test 8: TEST OK [0.000 secs, 4188 KB]
   Test 9: TEST OK [0.000 secs, 4188 KB]
   Test 10: TEST OK [0.000 secs, 4188 KB]

All tests OK.

YOUR PROGRAM ('milk3') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.



AC代码:
/*
ID:maxkibb3
LANG:C++
PROG:milk3
*/

#include<cstdio>

int v[3];
int a[3];
bool vis[21][21][21];

int min(int a, int b) {
	return (a < b)?a:b;	
}

void dfs(int x[3]) {
	if(vis[x[0]][x[1]][x[2]]) return;
	vis[x[0]][x[1]][x[2]] = true;
	for(int i = 0; i < 3; i++) {
		for(int j = 0; j < 3; j++) {
			if(i == j) continue;
			if(x[i] != 0 && x[j] != v[j]) {
				int amt = min(x[i], v[j] - x[j]);
				x[i] -= amt; x[j] += amt;
				dfs(x);
				x[i] += amt; x[j] -= amt;
			}
		}
	}
}

int main() {
	freopen("milk3.in", "r", stdin);
	freopen("milk3.out", "w", stdout);
	scanf("%d%d%d", &v[0], &v[1], &v[2]);
	a[0] = a[1] = 0; a[2] = v[2];
	dfs(a);
	for(int i = 0; i < v[2]; i++) {
		for(int j = 0; j <= v[2]; j++) {
			if(vis[0][j][i]) {
				printf("%d ", i);
				break;
			}
		}
	}
	printf("%d\n", v[2]);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值