uscao Mother's Milk
生活随笔
收集整理的這篇文章主要介紹了
uscao Mother's Milk
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
bfs吧,我做過一題非常可樂跟這題很像。就是每次把牛奶在兩個杯子中互倒,找到所有情況把a==0這個情況下的c保存起來(可能不同狀態下的c相同用vis標記)。
/*
ID: jinbo wu
LANG: C++
TASK: milk3
*/
#include<bits/stdc++.h>
using namespace std;
bool v[25][25][25];
bool vis[25];
struct l
{
int a,b,c;
};
int A,B,C;
int ans[25];
int len;
queue<l> q2;
void bfs()
{
l t1,t2;
len=0;
t1.a=0,t1.b=0,t1.c=C;
q2.push(t1);
while(!q2.empty())
{
t2=q2.front();
q2.pop();
if(t2.a==0)
{
if(!vis[t2.c])
{
ans[len++]=t2.c;
vis[t2.c]=1;}
}
//a-b
int temp=min(t2.a,B-t2.b);
t1.a=t2.a-temp,t1.b=t2.b+temp,t1.c=t2.c;
if(!v[t1.a][t1.b][t1.c])
{
q2.push(t1);
v[t1.a][t1.b][t1.c]=1;}
//b-a
temp=min(t2.b,A-t2.a);
t1.a=t2.a+temp,t1.b=t2.b-temp,t1.c=t2.c;
if(!v[t1.a][t1.b][t1.c])
{q2.push(t1);v[t1.a][t1.b][t1.c]=1;}//a-ctemp=min(t2.a,C-t2.c);t1.a=t2.a-temp,t1.c=t2.c+temp,t1.b=t2.b;if(!v[t1.a][t1.b][t1.c])
{
q2.push(t1);
v[t1.a][t1.b][t1.c]=1;}//c-atemp=min(t2.c,A-t2.a);t1.a=t2.a+temp,t1.c=t2.c-temp,t1.b=t2.b;if(!v[t1.a][t1.b][t1.c])
{
q2.push(t1);
v[t1.a][t1.b][t1.c]=1;}
//b-c
temp=min(t2.b,C-t2.c);
t1.b=t2.b-temp,t1.c=t2.c+temp,t1.a=t2.a;
if(!v[t1.a][t1.b][t1.c])
{
q2.push(t1);
v[t1.a][t1.b][t1.c]=1;}//c-btemp=min(t2.c,B-t2.b);t1.b=t2.b+temp,t1.c=t2.c-temp,t1.a=t2.a;if(!v[t1.a][t1.b][t1.c])
{
q2.push(t1);
v[t1.a][t1.b][t1.c]=1;}
}}
int main()
{
int a,b,c;
freopen("milk3.in","r",stdin);
freopen("milk3.out","w",stdout);
scanf("%d %d %d",&A,&B,&C);
bfs();
sort(ans,ans+len);
for(int i=0;i<len-1;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[len-1]);}
總結
以上是生活随笔為你收集整理的uscao Mother's Milk的全部內容,希望文章能夠幫你解決所遇到的問題。