USACO1.4 Mother's milk

很久以前遗留的模拟题……

完全无节操手动模拟…

于是代码N冗长

主要就两种操作,一个是从有牛奶的a瓶倒向b瓶子和c瓶子

另一个是从有牛奶的a瓶倒向b瓶子或c瓶子

用一个vis数组记录a b c的状态,如果这个状态没有出现过的话就继续搜,否则跳出。

注意题目要求,开始是C瓶子是满的,A B是空。

求当A是空瓶子的时候C还有多少牛奶

/*
ID: jim_jim1
PROG: milk3
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxx = 25;
bool vis[maxx][maxx][maxx];
struct bottle
{
    int a[2],b[2],c[2];
};

bool num[21];

void pour(int a[],int b[],int c[])//把a里的牛奶先倒到b,再倒到c
{
    if(a[1] >= b[0] - b[1])
    {
        a[1] -= (b[0]-b[1]);
        b[1] = b[0];
    }
    else
    {
        b[1]+=a[1];
        a[1] = 0;
    }

    if(a[1] >= c[0] - c[1])
    {
        a[1] -= (c[0]-c[1]);
        c[1] = c[0];
    }
    else
    {
        c[1]+=a[1];
        a[1] = 0;
    }
}

void pour2(int a[],int b[])//把a里的牛奶倒到b
{
    if(a[1] >= b[0] - b[1])
    {
        a[1] -= (b[0]-b[1]);
        b[1] = b[0];
    }
    else
    {
        b[1]+=a[1];
        a[1] = 0;
    }
}

void check(bottle x)
{
    if(x.a[1]==0)
        num[x.c[1]] = 1;
}

void back(bottle &p, bottle &q)//p is a copy of q
{
    for(int i=0;i<2;i++)
    {
        p.a[i] = q.a[i];
        p.b[i] = q.b[i];
        p.c[i] = q.c[i];
    }
}

int main()
{
    freopen("milk3.in","r",stdin);
    freopen("milk3.out","w",stdout);
    bottle one;
    while(scanf("%d%d%d",&one.a[0],&one.b[0],&one.c[0])!=EOF)
    {
        memset(num,0,sizeof(num));
        memset(vis,0,sizeof(vis));
        one.c[1] = one.c[0], one.a[1] = one.b[1] = 0;
        queue<bottle> q;
        q.push(one);
        while(!q.empty())
        {
            bottle tem = q.front();
            q.pop();
            check(tem);
            bottle tem2;
            if(tem.a[1]>0)
            {
                back(tem2,tem);
                pour(tem2.a,tem2.b,tem2.c);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }

                back(tem2,tem);
                pour(tem2.a,tem2.c,tem2.b);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }

                back(tem2,tem);
                pour2(tem2.a,tem2.c);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }
                back(tem2,tem);
                pour2(tem2.a,tem2.b);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }
            }
            if(tem.b[1]>0)
            {
                back(tem2,tem);
                pour(tem2.b,tem2.a,tem2.c);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }
                back(tem2,tem);
                pour(tem2.b,tem2.c,tem2.a);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }

                back(tem2,tem);
                pour2(tem2.b,tem2.a);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }
                back(tem2,tem);
                pour2(tem2.b,tem2.c);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }
            }
            if(tem.c[1]>0)
            {
                back(tem2,tem);
                pour(tem2.c,tem2.a,tem2.b);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }
                back(tem2,tem);
                pour(tem2.c,tem2.b,tem2.a);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }

                back(tem2,tem);
                pour2(tem2.c,tem2.a);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }
                back(tem2,tem);
                pour2(tem2.c,tem2.b);
                if(!vis[tem2.a[1]][tem2.b[1]][tem2.c[1]])
                {
                    vis[tem2.a[1]][tem2.b[1]][tem2.c[1]] = 1;
                    q.push(tem2);
                }
            }
        }
        int counter = 0;
        for(int i=0;i<=20;i++)
        {
            if(num[i])
            {
                if(!counter)
                {
                    printf("%d",i);
                    counter++;
                }
                else
                {
                    printf(" %d",i);
                }
            }
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值