USACO: Mother's Milk

中文题目:http://www.nocow.cn/index.php/Translate:USACO/milk3

搜索题, 可以用dfs做,每一步都有6种选择,a->b, b->a, a->c...............

因为牛奶总数不变,三维的状态(a,b,c) 可以降至二维的(a,b)。 用数组记录已经访问过的状态,如果访问过了,就返回。

/*
PROG: milk3
LANG: C++11
*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define eps  10e-8

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){
    // when to terminate
    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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值