USACO 1.4 Mother's Milk (milk3)

/*
ID: haolink1
PROG: milk3
LANG: C++
*/

//#include <iostream>
#include <fstream>
using namespace std;
//Use global varible to reduce the function DFS's aguments
const short  max_value = 20;
bool bucket_state[max_value+1][max_value+1];
short a_capacity = 0;
short b_capacity = 0;
short c_capacity = 0;
bool c_remain[max_value+1] = {false}; 
short Max(short first,short second){
    if(first >= second)
        return first;
    else return second;
}

short Min(short first,short second){
    if(first <= second)
        return first;
    else return second;
}
void DFS(short a,short b,short c){
    if(bucket_state[a][b] == true)
        return;
    bucket_state[a][b] = true;
    if(a == 0)
       c_remain[c] = true; 
    //There are total 6 cases to pour bucket.Once pouring begin,it won't end unless
    //either the soucr bucket is empty or the target bucket is full.
    //a->b
    if(a > 0 && b < b_capacity)
        DFS(Max(0,a+b-b_capacity),Min(b_capacity,a+b),c);
    //a->c
    if(a > 0 && c < c_capacity)
        DFS(Max(0,a+c-c_capacity),b,Min(c_capacity,a+c));
    //b->a
    if(b > 0 && a < a_capacity)
        DFS(Min(a_capacity,a+b),Max(0,a+b-a_capacity),c);
    //b->c
    if(b > 0 && c < c_capacity)
        DFS(a,Max(0,b+c-c_capacity),Min(c_capacity,b+c));
    //c->a
    if(c > 0 && a < a_capacity)   
        DFS(Min(a_capacity,a+c),b,Max(0,a+c-a_capacity));
    //c->b
    if(c > 0 && b < b_capacity)
        DFS(a,Min(b_capacity,b+c),Max(0,b+c-b_capacity));
}

int main(){
    ifstream fin("milk3.in");
    fin >> a_capacity;
    fin >> b_capacity;
    fin >> c_capacity;
    for(short i = 0; i < max_value; i++){
        for(short j = 0; j < max_value; j++){
            bucket_state[i][j] = false;
        }
    }
    DFS(0,0,c_capacity);
    ofstream fout("milk3.out");
    for(short i = 0; i < c_capacity; i++){
        if(c_remain[i] == true)
            fout << i <<" ";
    }
    // c_capacity is always qualified;
    fout<<c_capacity<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值