非常可乐
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2874 Accepted Submission(s): 1157
Problem Description
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
Input
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
Output
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
Sample Input
7 4 3 4 1 3 0 0 0
Sample Output
NO 3
Author
seeyou
Source
Recommend
LL
解题思路:每次倒的时候有6种倒法,即a->b,a->c,b->a,b->c,c->a,c->b,分倒满和倒不满两种情况讨论,记录下已出现的状态,避免重复搜索,然后BFS就可以了。
解题思路:每次倒的时候有6种倒法,即a->b,a->c,b->a,b->c,c->a,c->b,分倒满和倒不满两种情况讨论,记录下已出现的状态,避免重复搜索,然后BFS就可以了。
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
bool visit[105][105][105];
typedef struct cola
{
int a,b,c,v;
cola()
{
a=b=c=v=0;
}
};
bool pre(cola x,int y)
{
if(x.a==y/2&&x.b==y/2||x.c==y/2&&x.a==y/2||x.c==y/2&&x.b==y/2)
return true;
return false;
}
int BFS(cola x,int a,int b,int k)
{
cola now,temp;
queue<cola> c;
c.push(x);
while(!c.empty())
{
now=c.front();
c.pop();
if(now.b<a&&now.a>0)
{
temp.c=now.c;
temp.v=now.v+1;
if(now.a>a-now.b)
{
temp.a=now.a+now.b-a;
temp.b=a;
}
else
{
temp.b=now.b+now.a;
temp.a=0;
}
if(pre(temp,k))
return temp.v;
if(!visit[temp.a][temp.b][temp.c])
{
visit[temp.a][temp.b][temp.c]=true;
c.push(temp);
}
}
if(now.c<b&&now.a>0)
{
temp.v=now.v+1;
temp.b=now.b;
if(now.a>b-now.c)
{
temp.a=now.a+now.c-b;
temp.c=b;
}
else
{
temp.c=now.c+now.a;
temp.a=0;
}
if(pre(temp,k))
return temp.v;
if(!visit[temp.a][temp.b][temp.c])
{
visit[temp.a][temp.b][temp.c]=true;
c.push(temp);
}
}
if(now.a<k&&now.b>0)
{
temp.c=now.c;
temp.v=now.v+1;
if(now.b>k-now.a)
{
temp.b=now.b+now.a-k;
temp.a=k;
}
else
{
temp.a=now.a+now.b;
temp.b=0;
}
if(pre(temp,k))
return temp.v;
if(!visit[temp.a][temp.b][temp.c])
{
visit[temp.a][temp.b][temp.c]=true;
c.push(temp);
}
}
if(now.c<b&&now.b>0)
{
temp.a=now.a;
temp.v=now.v+1;
if(now.b>b-now.c)
{
temp.b=now.b+now.c-b;
temp.c=b;
}
else
{
temp.c=now.c+now.b;
temp.b=0;
}
if(pre(temp,k))
return temp.v;
if(!visit[temp.a][temp.b][temp.c])
{
visit[temp.a][temp.b][temp.c]=true;
c.push(temp);
}
}
if(now.a<k&&now.c>0)
{
temp.b=now.b;
temp.v=now.v+1;
if(now.c>k-now.a)
{
temp.c=now.c+now.a-k;
temp.a=k;
}
else
{
temp.a=now.a+now.c;
temp.c=0;
}
if(pre(temp,k))
return temp.v;
if(!visit[temp.a][temp.b][temp.c])
{
visit[temp.a][temp.b][temp.c]=true;
c.push(temp);
}
}
if(now.b<a&&now.c>0)
{
temp.a=now.a;
temp.v=now.v+1;
if(now.c>a-now.b)
{
temp.c=now.c+now.b-a;
temp.b=a;
}
else
{
temp.b=now.b+now.c;
temp.c=0;
}
if(pre(temp,k))
return temp.v;
if(!visit[temp.a][temp.b][temp.c])
{
visit[temp.a][temp.b][temp.c]=true;
c.push(temp);
}
}
}
return -1;
}
int main()
{
int p,q,r,ans;
cola ini;
while(scanf("%d%d%d",&p,&q,&r))
{
if(p==0&&q==0&&r==0)
break;
if(p%2)
{
printf("NO\n");
continue;
}
if(q==r)
{
printf("1\n");
continue;
}
memset(visit,false,sizeof(visit));
ini.a=p;ini.b=0;ini.c=0;
visit[p][0][0]=true;
ans=BFS(ini,q,r,p);
if(ans==-1)
printf("NO\n");
else
printf("%d\n",ans);
}
return 0;
}