很久以前遗留的模拟题……
完全无节操手动模拟…
于是代码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;
}