USACO Section 1.4 Mother's Milk(BFS)

Mother's Milk

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

题意:给你三个容器A、B、C,体积分别是va、vb、vc,其中C中是满的,A、B中是空的,现在有这样的一些操作把 x 容器中的牛奶到入 y 容器中要么是把 x 倒空,要么是把 y 倒满。在这些操作中 A容器为空时输出 C 中的牛奶。

分析:直接BFS,由于每个容器的体积是小于 20 的,所以可以用visited[21][21][21]这个数组来标记状态。

View Code
  1  /*
  2  ID: dizzy_l1
  3  LANG: C++
  4  TASK: milk3
  5  */
  6 #include<iostream>
  7 #include<algorithm>
  8 #include<cstring>
  9 #include<cstdio>
 10 #include<queue>
 11 
 12 using namespace std;
 13 
 14 struct state
 15 {
 16     int a,b,c;
 17 };
 18 int va,vb,vc,ans[1000],cnt;
 19 bool visited[21][21][21];
 20 
 21 bool judge(state s)
 22 {
 23     if(s.a==0&&s.b==0&&s.c==vc) return false;
 24     return true;
 25 }
 26 
 27 void BFS(state s)
 28 {
 29     state t,tt;
 30     memset(visited,false,sizeof(visited));
 31     queue<state>Q;
 32     while(!Q.empty()) Q.pop();
 33     Q.push(s);
 34     visited[s.a][s.b][s.c]=true;
 35     while(!Q.empty())
 36     {
 37         t=Q.front();Q.pop();
 38         if(t.a==0)
 39         {
 40             ans[cnt++]=t.c;
 41         }
 42         if(t.a)
 43         {
 44             if(t.b<vb)
 45             {
 46                 tt.a=t.a-min(t.a,vb-t.b);
 47                 tt.b=t.b+min(t.a,vb-t.b);
 48                 tt.c=t.c;
 49                 if(!visited[tt.a][tt.b][tt.c])
 50                     Q.push(tt);
 51                 visited[tt.a][tt.b][tt.c]=true;
 52             }
 53             if(t.c<vc)
 54             {
 55                 tt.a=t.a-min(t.a,vc-t.c);
 56                 tt.c=t.c+min(t.a,vc-t.c);
 57                 tt.b=t.b;
 58                 if(!visited[tt.a][tt.b][tt.c])
 59                     Q.push(tt);
 60                 visited[tt.a][tt.b][tt.c]=true;
 61             }
 62         }
 63         if(t.b)
 64         {
 65             if(t.a<va)
 66             {
 67                 tt.b=t.b-min(t.b,va-t.a);
 68                 tt.a=t.a+min(t.b,va-t.a);
 69                 tt.c=t.c;
 70                 if(!visited[tt.a][tt.b][tt.c])
 71                     Q.push(tt);
 72                 visited[tt.a][tt.b][tt.c]=true;
 73             }
 74             if(t.c<vc)
 75             {
 76                 tt.b=t.b-min(t.b,vc-t.c);
 77                 tt.c=t.c+min(t.b,vc-t.c);
 78                 tt.a=t.a;
 79                 if(!visited[tt.a][tt.b][tt.c])
 80                     Q.push(tt);
 81                 visited[tt.a][tt.b][tt.c]=true;
 82             }
 83         }
 84         if(t.c)
 85         {
 86             if(t.a<va)
 87             {
 88                 tt.a=t.a+min(t.c,va-t.a);
 89                 tt.b=t.b;
 90                 tt.c=t.c-min(t.c,va-t.a);
 91                 if(!visited[tt.a][tt.b][tt.c])
 92                     Q.push(tt);
 93                 visited[tt.a][tt.b][tt.c]=true;
 94             }
 95             if(t.b<vb)
 96             {
 97                 tt.a=t.a;
 98                 tt.b=t.b+min(t.c,vb-t.b);
 99                 tt.c=t.c-min(t.c,vb-t.b);
100                 if(!visited[tt.a][tt.b][tt.c])
101                     Q.push(tt);
102                 visited[tt.a][tt.b][tt.c]=true;
103             }
104         }
105     }
106 }
107 
108 int main()
109 {
110     freopen("milk3.in","r",stdin);
111     freopen("milk3.out","w",stdout);
112     int i;
113     while(scanf("%d%d%d",&va,&vb,&vc)==3)
114     {
115         cnt=0;
116         state t;
117         t.a=0;t.b=0;t.c=vc;
118         BFS(t);
119         sort(ans,ans+cnt);
120         for(i=0;i<cnt-1;i++)
121             printf("%d ",ans[i]);
122         printf("%d\n",ans[i]);
123     }
124     return 0;
125 }
posted @ 2012-08-28 15:46  mtry  阅读(553)  评论(0)    收藏  举报