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
题目大意
中文题
思路
A B C三个杯子,总共有12种情况
A到B够倒和不够倒两种;
A到C够倒和不够倒两种;
B到A够倒和不够倒两种;
B到C够倒和不够倒两种;
C到A够倒和不够倒两种;
C到B够倒和不够倒两种;
bfs这12中情况就可以了;
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int S,N,M;
int NUMBER;
bool Can;
int vis[101][101][101];
struct Tri
{
int s;
int n;
int m;
int number;
};
bool IsOver( int s,int n,int m )
{
if( (s == m && s != 0 && n == 0 )|| ( s == n && n != 0 && m==0 )|| ( m == n && m !=0 && s == 0 ) )
return true;
return false;
}
bool IsLeagal( int s,int n,int m )
{
if( s < 0 || s > S || n < 0 || n > N || m < 0 || m > M )
return false;
return true;
}
void bfs()
{
queue<Tri> Ways;
memset( vis,0,sizeof( vis));
vis[S][0][0] = 1;
Tri Now,Next1,Next2,Next3,Next4,Next5,Next6;
Now.s = S;
Now.n = 0;
Now.m = 0;
Now.number = 0;
Ways.push ( Now );
while( !Ways.empty ())
{
Now.s = Ways.front ().s ;
Now.n = Ways.front ().n ;
Now.m = Ways.front ().m ;
Now.number = Ways.front ().number ;
Ways.pop ();
if( Can )
return;
/* 从第一个杯子到给第二个杯子 */
if( Now.s > ( N - Now.n) )
{ Next1.s = Now.s - ( N - Now.n);
Next1.n = N;
Next1.m = Now.m;
Next1.number = Now.number + 1;}
else
{ Next1.s = 0;
Next1.n = Now.n + Now.s ;
Next1.m = Now.m;
Next1.number = Now.number + 1;}
if( IsLeagal( Next1.s ,Next1.n ,Next1.m ) && !vis[Next1.s ][Next1.n ][Next1.m ])
{
vis[Next1.s ][Next1.n ][Next1.m ] = 1;
if( IsOver( Next1.s ,Next1.n ,Next1.m ))
{ Can = true;
NUMBER = Next1.number ;
return; }
Ways.push ( Next1 );
}
/* 从第一个杯子到给第三个杯子 */
if( Now.s > ( M - Now.m ) )
{ Next2.s = Now.s - ( M - Now.m );
Next2.n = Now.n;
Next2.m = M;
Next2.number = Now.number + 1;}
else
{ Next2.s = 0;
Next2.n = Now.n;
Next2.m = Now.s + Now.m;
Next2.number = Now.number + 1;}
if( IsLeagal( Next2.s ,Next2.n ,Next2.m ) && !vis[Next2.s ][Next2.n ][Next2.m ])
{
vis[Next2.s ][Next2.n ][Next2.m ] = 1;
if( IsOver( Next2.s ,Next2.n,Next2.m ))
{ Can = true;
NUMBER = Next2.number ;
return; }
Ways.push ( Next2 );
}
/* 从第二个杯子到给第一个杯子 */
if( Now.n > ( S - Now.s ) )
{ Next3.s = S;
Next3.n = Now.n - ( S - Now.s );
Next3.m = Now.m;
Next3.number = Now.number + 1;}
else
{ Next3.s = Now.s + Now.n ;
Next3.n = 0;
Next3.m = Now.m;
Next3.number = Now.number + 1;}
if( IsLeagal( Next3.s ,Next3.n ,Next3.m ) && !vis[Next3.s ][Next3.n ][Next3.m ])
{
vis[Next3.s ][Next3.n ][Next3.m ] = 1;
if( IsOver( Next3.s ,Next3.n ,Next3.m ))
{ Can = true;
NUMBER = Next3.number ;
return; }
Ways.push ( Next3 );
}
/* 从第二个杯子到给第三个杯子 */
if( Now.n > ( M - Now.m ))
{ Next4.s = Now.s ;
Next4.n = Now.n - ( M - Now.m );
Next4.m = M;
Next4.number = Now.number + 1;}
else
{ Next4.s = Now.s ;
Next4.n = 0;
Next4.m = Now.n + Now.m ;
Next4.number = Now.number + 1;}
if( IsLeagal( Next4.s ,Next4.n ,Next4.m ) && !vis[Next4.s ][Next4.n ][Next4.m ])
{
vis[Next4.s ][Next4.n ][Next4.m ] = 1;
if( IsOver( Next4.s ,Next4.n ,Next4.m ))
{ Can = true;
NUMBER = Next4.number ;
return; }
Ways.push ( Next4 );
}
/* 从第三个杯子到给第一个杯子 */
if( Now.m - ( S - Now.s ) )
{ Next5.s = S;
Next5.n = Now.n ;
Next5.m = Now.m - ( S - Now.s );
Next5.number = Now.number + 1;}
else
{ Next5.s = Now.s + Now.m ;
Next5.n = Now.n ;
Next5.m = 0;
Next5.number = Now.number + 1;}
if( IsLeagal( Next5.s ,Next5.n ,Next5.m ) && !vis[Next5.s ][Next5.n ][Next5.m ])
{
vis[Next5.s ][Next5.n ][Next5.m ] = 1;
if( IsOver( Next5.s ,Next5.n ,Next5.m ))
{ Can = true;
NUMBER = Next5.number ;
return; }
Ways.push ( Next5 );
}
/* 从第三个杯子到给第二个杯子 */
if( Now.m > ( N - Now.n ))
{ Next6.s = Now.s;
Next6.n = N;
Next6.m = Now.m - ( N - Now.n );
Next6.number = Now.number + 1;}
else
{ Next6.s = Now.s;
Next6.n = Now.m + Now.n ;
Next6.m = 0;
Next6.number = Now.number + 1;}
if( IsLeagal( Next6.s ,Next6.n ,Next6.m ) && !vis[Next6.s ][Next6.n ][Next6.m ])
{
vis[Next6.s ][Next6.n ][Next6.m ] = 1;
if( IsOver( Next6.s ,Next6.n ,Next6.m ))
{ Can = true;
NUMBER = Next6.number ;
return; }
Ways.push ( Next6 );
}
}
}
int main()
{
while( cin >> S >> N >> M )
{
if( S == 0 && N == 0 && M == 0 ) break;
if( N > M )
{
int tmp;
tmp = N;
N =M;
M = tmp;
}
if( S % 2 != 0 )
{
cout<<"NO"<<endl;
continue;
}
Can = false;
bfs();
if( Can )
cout<< NUMBER <<endl;
else
cout<<"NO"<<endl;
}
return 0;
}