USACO 1.4 Mother's Milk

比较经典的3个杯子互相倒水的问题。

状态即3个杯子中的水量,用一个3维数组记录,转移为六种组合。

code
   
     
1 /*
2 ID: superbi1
3 LANG: C
4 TASK: milk3
5   */
6 // BFS
7 // 三个杯子A,B,C 刚开始,C中milk满的,然后根据规则倒,问A为空时的C的milk量的序列
8 #include < stdio.h >
9 #include < string .h >
10 #include < stdlib.h >
11
12 // 其实可以用三维数组记录状态
13 char flg[ 1000000 ];
14
15 main()
16 {
17 FILE * fin = fopen( " milk3.in " , " r " );
18 FILE * fout = fopen( " milk3.out " , " w " );
19 int a, b, c;
20 int milk[ 21 ];
21 int start = c;
22 int que[ 1000 ], top = 0 , mk;
23 int cur;
24 int I, a0, b0, c0, a1, b1, c1;
25 fscanf(fin, " %d%d%d " , & a, & b, & c);
26 memset(flg, 0 , sizeof (flg));
27 que[top ++ ] = c;
28 flg[c] = 1 ;
29 for (I = 0 ; I < 21 ; I ++ ) milk[I] = 0 ;
30 while (top > 0 ) {
31 cur = que[ -- top];
32 if ( ! (cur / 10000 )) {
33 milk[cur % 100 ] = 1 ;
34 }
35 a0 = cur / 10000 ;
36 b0 = (cur % 10000 ) / 100 ;
37 c0 = cur % 100 ;
38 a1 = a0;
39 b1 = b0;
40 c1 = c0;
41 // 枚举六种倒的情况
42 if (a1 > 0 ) {
43 if (b1 < b) {
44 if (b - b1 < a1) {
45 a1 -= b - b1;
46 b1 = b;
47 } else {
48 b1 += a1;
49 a1 = 0 ;
50 }
51 if ( ! flg[a1 * 10000 + b1 * 100 + c1]) {
52 flg[a1 * 10000 + b1 * 100 + c1] = 1 ;
53 que[top ++ ] = a1 * 10000 + b1 * 100 + c1;
54 }
55 }
56 a1 = a0;
57 b1 = b0;
58 c1 = c0;
59 if (c1 < c) {
60 if (c - c1 < a1) {
61 a1 -= c - c1;
62 c1 = c;
63 } else {
64 c1 += a1;
65 a1 = 0 ;
66 }
67 if ( ! flg[a1 * 10000 + b1 * 100 + c1]) {
68 flg[a1 * 10000 + b1 * 100 + c1] = 1 ;
69 que[top ++ ] = a1 * 10000 + b1 * 100 + c1;
70 }
71 }
72 }
73
74 a1 = a0;
75 b1 = b0;
76 c1 = c0;
77 if (b1 > 0 ) {
78 if (a1 < a) {
79 if (a - a1 < b1) {
80 b1 -= a - a1;
81 a1 = a;
82 } else {
83 a1 += b1;
84 b1 = 0 ;
85 }
86 if ( ! flg[a1 * 10000 + b1 * 100 + c1]) {
87 flg[a1 * 10000 + b1 * 100 + c1] = 1 ;
88 que[top ++ ] = a1 * 10000 + b1 * 100 + c1;
89 }
90 }
91 a1 = a0;
92 b1 = b0;
93 c1 = c0;
94 if (c1 < c) {
95 if (c - c1 < b1) {
96 b1 -= c - c1;
97 c1 = c;
98 } else {
99 c1 += b1;
100 b1 = 0 ;
101 }
102 if ( ! flg[a1 * 10000 + b1 * 100 + c1]) {
103 flg[a1 * 10000 + b1 * 100 + c1] = 1 ;
104 que[top ++ ] = a1 * 10000 + b1 * 100 + c1;
105 }
106 }
107 }
108
109 a1 = a0;
110 b1 = b0;
111 c1 = c0;
112 if (c1 > 0 ) {
113 if (a1 < a) {
114 if (a - a1 < c1) {
115 c1 -= a - a1;
116 a1 = a;
117 } else {
118 a1 += c1;
119 c1 = 0 ;
120 }
121 if ( ! flg[a1 * 10000 + b1 * 100 + c1]) {
122 flg[a1 * 10000 + b1 * 100 + c1] = 1 ;
123 que[top ++ ] = a1 * 10000 + b1 * 100 + c1;
124 }
125 }
126 a1 = a0;
127 b1 = b0;
128 c1 = c0;
129 if (b1 < b) {
130 if (b - b1 < c1) {
131 c1 -= b - b1;
132 b1 = b;
133 } else {
134 b1 += c1;
135 c1 = 0 ;
136 }
137 if ( ! flg[a1 * 10000 + b1 * 100 + c1]) {
138 flg[a1 * 10000 + b1 * 100 + c1] = 1 ;
139 que[top ++ ] = a1 * 10000 + b1 * 100 + c1;
140 }
141 }
142 }
143 }
144 mk = 0 ;
145 for (I = 0 ; I < 21 ; I ++ ) {
146 if (milk[I]) {
147 if ( ! mk) { fprintf(fout, " %d " , I); mk = 1 ; }
148 else fprintf(fout, " %d " , I);
149 }
150 }
151 fprintf(fout, " \n " );
152 exit( 0 );
153 }

 

你可能感兴趣的:(USACO)