USACO sec1.4 Mother's Milk

BFS

 1 /*

 2 PROG : milk3

 3 LANG : C++

 4 */

 5 # include <stdio.h>

 6 

 7 # define MAXN 20005

 8 

 9 /**********************************************/

10 int a[3], c[3], ss;

11 char vis[MAXN];

12 int Q[MAXN], front, rear;

13 int sol[25];

14 

15 void cal(int s)

16 {

17     a[0] = s%21;

18     a[1] = s/21%21;

19     a[2] = s/(21*21);

20 }

21 

22 int rcal(void)

23 {

24     return a[0]+a[1]*21+a[2]*21*21;

25 }

26 

27 /* x --> y */

28 void pour(int x, int y, int s)

29 {

30     int nst;

31     cal(s);

32     if (a[x]+a[y] <= c[y])

33         a[y] += a[x], a[x] = 0;

34     else

35         a[x] -= (c[y]-a[y]), a[y] = c[y];

36         

37     nst = rcal();

38     if (!vis[nst])

39     {

40         Q[rear++] = nst;

41         vis[nst] = 1;

42         if (!a[0]) sol[a[2]] = 1;

43     }

44 }

45 

46 void bfs(void)

47 {

48     int cur;

49     

50     front = 1, rear = 2;

51     Q[1] = ss, vis[ss] = 1;

52     while (front < rear)

53     {

54         cur = Q[front++];

55         pour(0, 1, cur), pour(1, 0, cur);

56         pour(1, 2, cur), pour(2, 1, cur);

57         pour(0, 2, cur), pour(2, 0, cur);

58     }

59 }

60 /**********************************************/

61 void solve()

62 {

63     int i, first = 1;

64     scanf("%d%d%d", &c[0], &c[1], &c[2]);

65     a[0] = a[1] = 0, a[2] = c[2];

66     ss = rcal();

67     for (i = 0; i <= 21; ++i) sol[i] = 0;

68     bfs();

69     sol[c[2]] = 1;

70     for (i = 0; i <= 21; ++i) if (sol[i])

71     {

72         if (first)

73             first = 0, printf("%d", i);

74         else

75             printf(" %d", i);

76     }

77     putchar('\n');

78 }

79 

80 int main()

81 {

82     freopen("milk3.in", "r", stdin);

83     freopen("milk3.out", "w", stdout);

84     

85     solve();

86     

87     fclose(stdin);

88     fclose(stdout);

89     

90     return 0;

91 }

 

你可能感兴趣的:(USACO)