usaco1.4.2 Mother's Milk

来源:互联网 发布:js怎么把数字变成汉字 编辑:程序博客网 时间:2024/06/03 18:18

一. 原题

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: milk3LANG: C++Compiling...Compile: OKExecuting...   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 automatedcongratulations.



AC代码:
/*ID:maxkibb3LANG: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;}


0 0
原创粉丝点击