USACO Section 1.4: Prob Mother's Milk

Problem State:

=============================================================================================================

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

=========================================================================================================================================

这题我做的时候状态定义不好,一开始设想是一个set<vector<int> >,这样时间耗太长,最好不要用set,这里看了别人的代码发现他用一个bool型的3维矩阵真是太巧妙了。
下面贴代码
 1 /*

 2 ID: yingzho1

 3 LANG: C++

 4 TASK: milk3

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <stdio.h>

14 #include <queue>

15 

16 using namespace std;

17 

18 bool visit[21][21][21];

19 

20 struct state {

21     int s[3];

22     void visited() {visit[s[0]][s[1]][s[2]] = true;}

23     bool checkVisit() {return visit[s[0]][s[1]][s[2]];}

24 }cur, tmp, quefron;

25 

26 void pour(state &tmp, int s, int d, int cap[3]) {

27     int left = cap[d] - tmp.s[d];

28     if (left == 0) return;

29     if (left > tmp.s[s]) {

30         tmp.s[d] += tmp.s[s];

31         tmp.s[s] = 0;

32     }

33     else {

34         tmp.s[d] += left;

35         tmp.s[s] -= left;

36     }

37 }

38 

39 int main() {

40     ofstream fout ("milk3.out");

41     ifstream fin ("milk3.in");

42 

43     int cap[3];

44     for (int i = 0; i < 3; i++) fin >> cap[i];

45     cur.s[0] = cur.s[1] = 0;

46     cur.s[2] = cap[2];

47     bool res[21] = {false};

48     cur.visited();

49     queue<state> S;

50     S.push(cur);

51     while (!S.empty()) {

52         quefron = S.front();

53         S.pop();

54         if (quefron.s[0] == 0) res[quefron.s[2]] = true;

55         for (int i = 0; i < 3; i++) {

56             for (int j = 0; j < 3; j++) {

57                 if (i == j) continue;

58                 tmp = quefron;

59                 pour(tmp, i, j, cap);

60                 if (!tmp.checkVisit()) {

61                     S.push(tmp);

62                     tmp.visited();

63                 }

64             }

65         }

66     }

67 

68     for (int i = 0; i <= cap[2]; i++) {

69         if (res[i]) {

70             fout << i;

71             if (i == cap[2]) fout << endl;

72             else fout << " ";

73         }

74     }

75 

76     return 0;

77 }

 

 

你可能感兴趣的:(USACO)