HDU-1495 非常可乐(BFS)

时间:2022-09-25 15:37:32

广搜的灵活应用题:

非常可乐

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

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
 //一共6种情况;all->big,all->small,big->small,big->all,small->all,small->big进行bfs搜索,如果未被搜到过,
//就入队列,如果出现两个瓶子里水相同就返回输出。(总水量为奇数时无解)
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
int s,n,m;
int step;
};
int hash[][][];
int s,n,m; int BFS()
{
queue<node>q;
node cur,next;
cur.s=s;
cur.n=;
cur.m=;
cur.step=;
hash[cur.s][cur.n][cur.m]=;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
if((cur.n== && cur.m==cur.s)||(cur.m== && cur.n==cur.s)||( cur.s== && cur.n==cur.m)) return cur.step;
if(cur.n!=)
{
if(cur.n>s-cur.s)
{
next.n=cur.n-(s-cur.s);
next.s=s;
next.m=cur.m;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.n=;
next.s=cur.s+cur.n;
next.m=cur.m;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
if(cur.n>m-cur.m)
{
next.n=cur.n-(m-cur.m);
next.m=m;
next.s=cur.s;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.n=;
next.m=cur.m+cur.n;
next.s=cur.s;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
} if(cur.m)
{
if(cur.m>s-cur.s)
{
next.m=cur.n-(s-cur.s);
next.s=s;
next.n=cur.n;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.m=;
next.s=cur.s+cur.m;
next.n=cur.n;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
if(cur.m>n-cur.n)
{
next.m=cur.m-(n-cur.n);
next.n=n;
next.s=cur.s;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.m=;
next.n=cur.m+cur.n;
next.s=cur.s;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
} if(cur.s)
{
if(cur.s>n-cur.n)
{
next.s=cur.s-(n-cur.n);
next.n=n;
next.m=cur.m;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.s=;
next.n=cur.s+cur.n;
next.m=cur.m;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
if(cur.s>m-cur.m)
{
next.s=cur.s-(m-cur.m);
next.m=m;
next.n=cur.n;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.s=;
next.m=cur.m+cur.s;
next.n=cur.n;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
}
} return ;
}
int main()
{
int ans;
while(scanf("%d%d%d",&s,&n,&m),s||n||m)
{
memset(hash,,sizeof(hash)); if(s%) {printf("NO\n");continue;} ans=BFS();
if(ans) printf("%d\n",ans);
else printf("NO\n");
}
return ;
}