USACO 1.4 Mother's Milk(BFS)

以前做过类似的,写的很复杂,搞了一晚上。。2Y。

  1 /*

  2 ID: cuizhe

  3 LANG: C++

  4 TASK: milk3

  5 */

  6 #include <cstdio>

  7 #include <cstring>

  8 #include <cmath>

  9 #include <algorithm>

 10 using namespace std;

 11 #define LL long long

 12 int p[31][31][31];

 13 int qua[1000],qub[1000],quc[1000];

 14 int o[100];

 15 int main()

 16 {

 17     int a,b,c,i,j,str,end,num;

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

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

 20     scanf("%d%d%d",&a,&b,&c);

 21     memset(p,0,sizeof(p));

 22     qua[1] = 0;qub[1] = 0;quc[1] = c;

 23     str = end = num = 1;

 24     p[0][0][c] = 1;

 25     while(str <= end)

 26     {

 27         j = 1;

 28         for(i = str;i <= end;i ++)

 29         {

 30             if(qua[i] != 0)

 31             {

 32                 if(qub[i]+qua[i] <= b&&!p[0][qub[i]+qua[i]][quc[i]])

 33                 {

 34                     qua[end+j] = 0;

 35                     qub[end+j] = qub[i]+qua[i];

 36                     quc[end+j] = quc[i];

 37                     p[0][qub[i]+qua[i]][quc[i]] = 1;

 38                     j ++;

 39                 }

 40                 else if(qub[i]+qua[i] > b&&!p[qua[i]-(b-qub[i])][b][quc[i]])

 41                 {

 42                     qua[end+j] = qua[i]-(b-qub[i]);

 43                     qub[end+j] = b;

 44                     quc[end+j] = quc[i];

 45                     p[qua[i]-(b-qub[i])][b][quc[i]] = 1;

 46                     j ++;

 47                 }

 48                 if(quc[i]+qua[i] <= c&&!p[0][qub[i]][quc[i]+qua[i]])

 49                 {

 50                     qua[end+j] = 0;

 51                     qub[end+j] = qub[i];

 52                     quc[end+j] = quc[i]+qua[i];

 53                     p[0][qub[i]][quc[i]+qua[i]] = 1;

 54                     j ++;

 55                 }

 56                 else if(quc[i]+qua[i] > c&&!p[qua[i]-(c-quc[i])][qub[i]][c])

 57                 {

 58                     qua[end+j] = qua[i]-(c-quc[i]);

 59                     qub[end+j] = qub[i];

 60                     quc[end+j] = c;

 61                     p[qua[i]-(c-quc[i])][qub[i]][c] = 1;

 62                     j ++;

 63                 }

 64             }

 65             if(qub[i] != 0)

 66             {

 67                 if(qub[i]+qua[i] <= a&&!p[qub[a]+qua[i]][0][quc[i]])

 68                 {

 69                     p[qub[a]+qua[i]][0][quc[i]] = 1;

 70                     qua[end+j] = qua[i]+qub[i];

 71                     qub[end+j] = 0;

 72                     quc[end+j] = quc[i];

 73                     j ++;

 74                 }

 75                 else if(qub[i]+qua[i] > a&&!p[a][qub[i]-(a-qua[i])][quc[i]])

 76                 {

 77                     p[a][qub[i]-(a-qua[i])][quc[i]] = 1;

 78                     qua[end+j] = a;

 79                     qub[end+j] = qub[i]-(a-qua[i]);

 80                     quc[end+j] = quc[i];

 81                     j ++;

 82                 }

 83                 if(qub[i]+quc[i] <= c&&!p[qua[i]][0][quc[i]+qub[i]])

 84                 {

 85                     p[qua[i]][0][quc[i]+qub[i]] = 1;

 86                     qua[end+j] = qua[i];

 87                     qub[end+j] = 0;

 88                     quc[end+j] = qub[i]+quc[i];

 89                     j ++;

 90                 }

 91                 else if(qub[i]+quc[i] > c&&!p[qua[i]][qub[i]-(c-quc[i])][c])

 92                 {

 93                     p[qua[i]][qub[i]-(c-quc[i])][c] = 1;

 94                     qua[end+j] = qua[i];

 95                     qub[end+j] = qub[i]-(c-quc[i]);

 96                     quc[end+j] = c;

 97                     j ++;

 98                 }

 99             }

100             if(quc[i] != 0)

101             {

102                 if(quc[i]+qua[i] <= a&&!p[qua[i]+quc[i]][qub[i]][0])

103                 {

104                     p[qua[i]+quc[i]][qub[i]][0] = 1;

105                     qua[end+j] = quc[i]+qua[i];

106                     qub[end+j] = qub[i];

107                     quc[end+j] = 0;

108                     j ++;

109                 }

110                 else if(quc[i]+qua[i] > a&&!p[a][qub[i]][quc[i]-(a-qua[i])])

111                 {

112                     p[a][qub[i]][quc[i]-(a-qua[i])] = 1;

113                     qua[end+j] = a;

114                     qub[end+j] = qub[i];

115                     quc[end+j] = quc[i]-(a-qua[i]);

116                     j ++;

117                 }

118                 if(quc[i]+qub[i] <= b&&!p[qua[i]][qub[i]+quc[i]][0])

119                 {

120                     p[qua[i]][qub[i]+quc[i]][0] = 1;

121                     qua[end+j] = qua[i];

122                     qub[end+j] = qub[i]+quc[i];

123                     quc[end+j] = 0;

124                     j ++;

125                 }

126                 else if(quc[i]+qub[i] > b&&!p[qua[i]][b][quc[i]-(b-qub[i])])

127                 {

128                     p[qua[i]][b][quc[i]-(b-qub[i])] = 1;

129                     qua[end+j] = qua[i];

130                     qub[end+j] = b;

131                     quc[end+j] = quc[i]-(b-qub[i]);

132                     j ++;

133                 }

134             }

135         }

136         str = end+1;

137         end = end+j-1;

138     }

139     int z = 1;

140     for(i = 0;i <= c;i ++)

141     {

142         for(j = 0;j <= b;j ++)

143         {

144             if(p[0][j][i]&&!o[i])

145             {

146                 if(z)

147                 {

148                     printf("%d",i);

149                     z = 0;

150                 }

151                 else

152                 printf(" %d",i);

153                 o[i] = 1;

154             }

155         }

156     }

157     printf("\n");

158     return 0;

159 }

你可能感兴趣的:(USACO)