Categories

USACO 1.4.4 Mother's Milk

DFS,

设res[j]为C桶中牛奶量为j的可能性

status[i][j]为A桶有i牛奶,C桶有j牛奶的可能性,初态为i==0;j==C;

当i==0时置res[j]=true;

总共有六种倒法A->B,A->C,B->A,B->C,C->A,C->B

对每种status,分别搜索这六种倒法.

源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
ID: tgh7281
LANG: C++
TASK: milk3
*/
 
#include <iostream>
#include <fstream>
using namespace std;
 
void search(int ma,int mc);
bool res[21]={0};
bool status[21][21]={0}; //status[i][j]表示A有i牛奶,C有j牛奶的可能
int a,b,c,p,q;
 
int main()
{
    ifstream fin("milk3.in");
    ofstream fout("milk3.out");
 
    int s=0,i,j=0;
    fin>>a>>b>>c;
    search(0,c);
 
    for (i=0;i<=20;i++) if (res[i]) s++;
    for (i=0;i<=20;i++)
    {
        if (res[i])
        {
            fout<<i;
            j++;
            if (j==s) fout<<endl;
            else fout<<’ ‘;
        }
    }
 
    fin.close();
 
    fout.close();
 
    return 0;
}
 
void search(int ma,int mc)
{
    if (status[ma][mc]) return;
 
    int mb=c-ma-mc;
    status[ma][mc]=true;
    if (ma==0) res[mc]=true;
 
    search(ma-min(ma,b-mb),mc); //a->b
    search(ma-min(ma,c-mc),mc+min(ma,c-mc)); //a->c
    search(ma+min(mb,a-ma),mc); //b->a
    search(ma,mc+min(mb,c-mc)); //b->c
    search(ma+min(a-ma,mc),mc-min(a-ma,mc)); //c->a ma + min(mc,a-ma),
    search(ma,mc-min(b-mb,mc)); //c->b
}

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

*