非常可乐bfs和数学解法

8 篇文章 0 订阅

非常可乐

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28523    Accepted Submission(s): 11017


 

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

“2006校园文化活动月”之“校庆杯”大学生程序设计竞赛暨杭州电子科技大学第四届大学生程序设计竞赛

 

 

Recommend

LL

思路:bfs枚举所有倒水状况

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct node{
	int x,y,z,step;
	node(){
	}
	node(int xx,int yy,int zz,int sstep):x(xx),y(yy),z(zz),step(sstep){}
};
int a,b,c;
int book[109][109][109];
int bfs(int x,int y,int z,int step)
{
	queue<node>A;
	A.push(node(x,y,z,step));
	book[x][y][z] = 1;
	if(x == y+z)
	{
		return step;
	}
	while(!A.empty())
	{
		int xx,yy,zz;
		node u = A.front();
		A.pop();
		for(int i=0;i<6;i++)
		{
			if(u.x == u.y+u.z)
			{
				return u.step;
			}
			if(i == 0)
			{
				if(u.x == 0 || u.y == b)
				{
					continue;
				}
				 xx = 0;
				 yy = u.y+u.x;
				 zz = u.z;
				if(yy > b)
				{
					yy = b;
					xx = u.x-(b-u.y);
				}
			}
			else if(i == 1)
			{
				if(u.x == 0 || u.z == c)
				{
					continue;
				}
				 xx = 0;
				 yy = u.y;
				 zz = u.z+u.x;
				if(zz>c)
				{
					zz = c;
					xx = u.x-(c-u.z);	
				}	
			}
			else if(i == 2)
			{
				if(u.y == 0 || u.x == a)
				{
					continue;
				}
				 xx = u.x+u.y;
				 yy = 0;
				 zz = u.z ;
				if(xx>a)
				{
					xx = a;
					yy = u.y-(a-u.x); 	
				}	
			}
			else if(i == 3)
			{
				if(u.y == 0 || u.z == c)
				{
					continue;
				}
				 xx = u.x;
				 yy = 0;	
				 zz = u.y + u.z;
				if(zz > c)
				{
					zz = c;
					yy = u.y - (c-u.z); 
				}
			}
			else if(i == 4)
			{
				if(u.z == 0 || u.x == a)
				{
					continue;
				}
				 xx = u.x+u.z;
				 yy = u.y;
				 zz = 0;
				if(xx > a)
				{
					xx = a;
					zz = u.x - (a - u.x);
				}	
			}
			else
			{
				if(u.z == 0 || u.y == b)
				{
					continue;
				}
				 xx = u.x;
				 yy = u.z+u.y;
				 zz = 0;
				if(yy>b)
				{
					yy = b;
					zz = u.z - (b - u.y);
				}
			}
			int ss = u.step+1; 
			if(book[xx][yy][zz] == 0)
			{
				book[xx][yy][zz] = 1;
				A.push(node(xx,yy,zz,ss));
			}
		}	
	}
	
	return -1;
}
int main()
{
	
	while(~scanf("%d%d%d",&a,&b,&c))
	{
		memset(book,0,sizeof(book));
		if(a == 0 && b == 0 && c == 0)
		{
			return 0;
		}
		int g = bfs(a,0,0,0);
		if(g == -1)
		{
			printf("NO\n");continue;
		}
		else{
			printf("%d\n",g);
		}
	}
	return 0;
 } 

 

数论解法:

#include<iostream>
#include<algorithm> 
using namespace std;
int main()
{
	int a,b,c;
	while(~scanf("%d%d%d",&a,&b,&c))
	{
		if(a == 0 && b == 0 && c == 0)
		{
			return 0;
		}
		a/=__gcd(b,c);
		if(a&1)
		{
			printf("NO\n");continue;
		}
		else{
			printf("%d\n",a-1);continue;
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-lyslyslys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值