Codeup墓地-2118
src:http://codeup.cn/problem.php?id=2118
2118: 非常可乐
时间限制: 1 Sec 内存限制: 32 MB
题目描述
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升(正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
输入
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
输出
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
样例输入
7 4 2
6 3 3
6 4 2
6 1 1
6 4 1
0 0 0
样例输出
NO
1
NO
NO
3
//不一样的BFS
//http://blog.csdn.net/qq_34374664/article/details/51646060
#include<bits/stdc++.h>
using namespace std;
const int maxs=101;
int cup[3],half;
bool inq[maxs][maxs][maxs];
struct Node{
int cur[3],step;
};
void BFS(int s){
queue<Node>q;
Node p;p.cur[0]=s;p.cur[1]=p.cur[2]=p.step=0;
q.push(p);
inq[cup[0]][0][0]=true;
while(!q.empty()){
Node x=q.front();q.pop();
int sum=0;
for(int i=0;i<3;i++){
if(x.cur[i]==half)sum++;
}
if(sum==2){printf("%d\n",x.step);return;}
for(int i=0;i<3;i++){
if(x.cur[i]>0){
for(int j=0;j<3;j++){
if(i==j)continue;
Node newx=x;
if(newx.cur[i]>=cup[j]-newx.cur[j]){
newx.cur[i]-=cup[j]-newx.cur[j];
newx.cur[j]=cup[j];
}else{
newx.cur[j]+=newx.cur[i];
newx.cur[i]=0;
}
if(!inq[newx.cur[0]][newx.cur[1]][newx.cur[2]]){
newx.step++;
q.push(newx);
inq[newx.cur[0]][newx.cur[1]][newx.cur[2]]=true;
}
}
}
}
}
printf("NO\n");
}
int main(){
while(scanf("%d%d%d",&cup[0],&cup[1],&cup[2])!=EOF&&cup[0]&&cup[1]&&cup[2]){
memset(inq,0,sizeof inq);
if(cup[0]&1)printf("NO\n");
else{
half=(cup[0]>>1);
BFS(cup[0]);
}
}
return 0;
}