USACO Section 1.4: Mother's Milk

这题其实暴力是可以的,因为给的限制条件就很小了,难点在于编程,struct state的写法和pour函数都是关键点

 1 /*

 2 ID: leetcod3

 3 PROG: milk3

 4 LANG: C++

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <queue>

14 #include <cmath>

15 #include <list>

16 #include <cstring>

17 #include <cstdlib>

18 #include <limits>

19 #include <stack>

20 

21 using namespace std;

22 

23 ofstream fout ("milk3.out");

24 ifstream fin ("milk3.in");

25 

26 bool visit[21][21][21] = {false};

27 bool ans[21] = {false};

28 int c[3];

29 struct state {

30     int a[3];

31     void getvisit() {visit[a[0]][a[1]][a[2]] = true;}

32     bool isvisit() {return visit[a[0]][a[1]][a[2]];}

33 }cur;

34 queue<state> S;

35 

36 void pour(state &t, int s, int d) {

37     int tmp = c[d] - t.a[d];

38     if (tmp == 0) return;

39     else if (t.a[s] <= tmp) {

40         t.a[d] += t.a[s];

41         t.a[s] = 0;

42     }

43     else {

44         t.a[d] = c[d];

45         t.a[s] -= tmp;

46     }

47 }

48 

49 int main()

50 {

51     fin >> c[0] >> c[1] >> c[2];

52     cur.a[0] = cur.a[1] = 0;

53     cur.a[2] = c[2];

54     cur.getvisit();

55     S.push(cur);

56     while (!S.empty()) {

57         state front = S.front();

58         S.pop();

59         if (front.a[0] == 0) ans[front.a[2]] = true;

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

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

62                 if (i == j) continue;

63                 state tmp = front;

64                 pour(tmp, i, j);

65                 if (!tmp.isvisit()) {

66                     S.push(tmp);

67                     tmp.getvisit();

68                     //cout << "from state: " << front.a[0] << " " << front.a[1] << " " << front.a[2];

69                     //cout << " to state: " << tmp.a[0] << " " << tmp.a[1] << " " << tmp.a[2] << endl;

70                 }

71             }

72         }

73     }

74     for (int i = 0; i <= c[2]; i++) {

75         if (ans[i]) {

76             fout << i;

77             if (i == c[2]) fout << endl;

78             else fout << " ";

79         }

80     }

81     return 0;

82 }

 

你可能感兴趣的:(USACO)