Mother's Milk

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mother's Milk相关的知识,希望对你有一定的参考价值。

本题中,

三个桶,因此就是6种:A->B A->C B->A B->C C->A C->B。所有转移都考虑一遍即可。

以A->B来说,这样转移之后状态是什么呢?A的

牛奶数应该是max(0, a+b-B),其中a,b是A和B桶中原来牛奶的数量,A,B是A和B桶的容量。B的牛奶数是

min(a+b, B),其他的以此类推。本题就差不多了。~

代码:

 

/*
ID:Andy Chen
PROG: milk3
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include<vector>
#include<fstream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 100010;
const ll MOD = 1000000007;
const int INF = 0x7fffffff;

bool state[21][21];
bool record[21] = {false};
vector<int> ans;
int a, b, c;

void dfs(int x, int y){
    
    int c1 = c - x - y; 
    if(state[x][y]) return;
    if( !record[c1] && c1 >= c-b) ans.push_back(c1);
    record[c1] = true;
    state[x][y] = true;
    // a -> b
    int b1 = min(b , x + y);
    int a1 = x + y - b1;
    dfs(a1, b1);
    // b -> a
    a1 = min(a, x + y);
    b1 = x + y - a1;
    dfs(a1, b1);
    // a -> c
    a1 = 0;     // total milk = c
    b1 = y;
    dfs(a1, b1);
    // c -> a
    a1 = min(a, x + c1);
    b1 = y;
    dfs(a1, b1);
    // b -> c
    a1 = x;
    b1 = 0;
    dfs(a1, b1);
    // c -> b
    a1 = x;
    b1 = min(b, y + c1);
    dfs(a1, b1);
}

int main()
{
    ifstream fin("milk3.in");
    ofstream fout("milk3.out");

    fin >> a >> b >> c;
    dfs(0, 0);
    sort(ans.begin(), ans.end());
    for(int i = 0; i <= ans.size()-2; i++){
        fout << ans[i] << " ";
    }
    fout << ans[ans.size()-1] << endl;
    return 0;
}

 

以上是关于Mother's Milk的主要内容,如果未能解决你的问题,请参考以下文章

Mother's Milk

USACO1.5 Mother's Milk搜索

[USACO1.4]母亲的牛奶 Mother's Milk

[USACO1.4]母亲的牛奶 Mother's Milk

洛谷 P1215 [USACO1.4]母亲的牛奶 Mother's Milk

洛谷 P1215 [USACO1.4]母亲的牛奶 Mother's Milk