非常可乐
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4623 Accepted Submission(s): 1853
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
bfs 所有状态
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
int s,n,m,ans;
struct Node{
int x,y,z,cnt;
Node(){}
Node(int a,int b,int c,int time):x(a),y(b),z(c),cnt(time){}
};
bool mark[110][110];
queue< Node > q;
bool check()
{
int a,b,c,t;
a=q.back().x,b=q.back().y,c=q.back().z,t=q.back().cnt;
if(a==b&&c==0) {
printf("%d\n",t);
return 1;
}
if(a==c&&b==0)
{
printf("%d\n",t);
return 1;
}
if(b==c&&a==0)
{
printf("%d\n",t);
return 1;
}
return 0;
}
void bfs()
{
//ans=s/2;
Node temp;
temp=Node(s,0,0,0);
mark[temp.x][temp.y]=0;
q.push(temp);
while (!q.empty())
{
int a,b,c,t;
a=q.front().x,b=q.front().y,c=q.front().z,t=q.front().cnt;
//printf("%d %d %d %d",a,b,c,t);
if (a<s) //如果能a内加水
{
if(b>0) //b内有水
{
if(a+b<s)//b加进a都加不满
{
if(mark[a+b][0]) {
temp=Node(a+b,0,c,t+1);
mark[a+b][0]=0;
q.push(temp);
if(check()) break;
}
}
else //b还剩一点
{
if(mark[s][a+b-s])
{
temp=Node(s,a+b-s,c,t+1);
mark[s][a+b-s]=0;
q.push(temp);
if(check()) break;
}
}
}
if(c>0) //there is water in c
{
if(a+c<s)// the sum of (a and c) is less than s
{
if(mark[a+c][b]) {
temp=Node(a+c,b,0,t+1);
mark[a+c][b]=0;
q.push(temp);
if(check()) break;
}
}
else // more than s
{
if(mark[s][b])
{
temp=Node(s,b,a+c-s,t+1);
mark[s][b]=0;
q.push(temp);
if(check()) break;
}
}
}
}
if(b<n) // b cup is not full
{
if(a>0) //there is some water in cup a
{
if(a+b<n)//the sum of a and b is less than n
{
if(mark[0][a+b]) {
temp=Node(0,a+b,c,t+1);
mark[0][a+b]=0;
q.push(temp);
if(check()) break;
}
}
else //bigger
{
if(mark[a+b-n][n])
{
temp=Node(a+b-n,n,c,t+1);
mark[a+b-n][n]=0;
q.push(temp);
if(check()) break;
}
}
}
if(c>0) // there is some water in cup c
{
if(b+c<n) // sum(b + c) less tan n
{
if(mark[a][b+c]) {
temp=Node(a,b+c,0,t+1);
mark[a][c+b]=0;
q.push(temp);
if(check()) break;
}
}
else // bigger
{
if(mark[a][n])
{
temp=Node(a,n,b+c-n,t+1);
mark[a][n]=0;
q.push(temp);
if(check()) break;
}
}
}
}
if(c<m) // cup c is not full
{
if(a>0)//there is water in cup a
{
if(a+c<m)//sum of (a and c)less than m
{
if(mark[0][b]) {
temp=Node(0,b,a+c,t+1);
mark[0][b]=0;
q.push(temp);
if(check()) break;
}
}
else//bigger
{
if(mark[a+c-m][b])
{
temp=Node(a+c-m,b,m,t+1);
mark[a+c-m][b]=0;
q.push(temp);
if(check()) break;
}
}
}
if(b>0) //there is water in b
{
if(b+c<m) //sum of( b+c ) less than ,
{
if(mark[a][0]) {
temp=Node(a,0,b+c,t+1);
mark[a][0]=0;
q.push(temp);
if(check()) break;
}
}
else //bigger than m
{
if(mark[a][b+c-m])
{
temp=Node(a,b+c-m,m,t+1);
mark[a][b+c-m]=0;
q.push(temp);
if(check()) break;
}
}
}
}
//cout<<q.size()<<endl;
//printf("%d %d %d %d\n",a,b,c,t);
q.pop();
}
if(q.empty()) puts("NO");
return ;
}
void init()
{
while (!q.empty()) q.pop();
memset(mark,1,sizeof(mark));
return ;
}
int main ()
{
//freopen("date.in","r",stdin);
//freopen("date.out","w",stdout);
while (1)
{
init();
scanf("%d%d%d",&s,&n,&m);
if(s+n+m==0) break;
if(s%2){ puts("NO"); continue;}
bfs();
}
return 0;
}
通过广度优先搜索算法解决两个不同容量杯子如何平均分配特定总量可乐的问题。输入为可乐总量及两个杯子的容量,输出最少倒可乐次数或无法平分的信息。
1134

被折叠的 条评论
为什么被折叠?



