原帖链接:http://blog.csdn.net/ilovexiaohao/article/details/8564830
可乐体积为S,互相之间来回倾倒饮料,判断是否可以将S平分。广搜所有的状态即可。
这份代码的优越在于二层for循环的使用,有效避免的冗长重复的代码。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=100+5;
bool hash[maxn][maxn];//由于总体积为S,当两个瓶子里的饮料体积固定时,第三个必然固定
int a[5];//每个瓶子的体积
struct node
{
int v[3],step;//三个瓶子里饮料的体积
};
void BFS()
{
node cur,next;
cur.v[0]=a[0];
cur.v[1]=cur.v[2]=0;
cur.step=0;
queue<node>q;
memset(hash,0,sizeof(hash));
hash[0][0]=1;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
if((cur.v[0]==a[0]/2&&cur.v[1]==a[0]/2)||(cur.v[0]==a[0]/2&&cur.v[2]==a[0]/2)||(cur.v[1]==a[0]/2&&cur.v[2]==a[0]/2))
{
printf("%d\n",cur.step);
return ;
}
for(int i=0; i<3; i++)//i瓶向j瓶倒饮料
if(cur.v[i]>0)//如果i瓶有饮料
for(int j=0; j<3; j++)
{
next=cur;
if(i==j) continue;
if(next.v[i]+next.v[j]>a[j])//j瓶倒满后i瓶会有剩余
{
next.v[i]=next.v[i]-(a[j]-next.v[j]);
next.v[j]=a[j];
}
else//i瓶全部倒进j瓶中
{
next.v[j]+=next.v[i];
next.v[i]=0;
}
if(!hash[next.v[1]][next.v[2]])//该状态没有出现
{
hash[next.v[1]][next.v[2]]=1;
next.step++;
q.push(next);
}
}
}
printf("NO\n");
}
int main()
{
while(~scanf("%d%d%d",&a[0],&a[1],&a[2]),a[0]||a[1]||a[2])
{
if(a[0]%2)
{
puts("NO");
continue;
}
if(a[1]==a[2])
{
puts("1");
continue;
}
BFS();
}
return 0;
}