标签:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
int a[3];
int len;
};
queue<node> q;
int map[101][101][101];
int dx[3];
double flag;
void bfs(int x,int y,int z)
{
while(!q.empty())
{
q.pop();
}
node p;
p.a[0]=x;
p.a[1]=y;
p.a[2]=z;
p.len=0;
q.push(p);
while(!q.empty())
{
node p=q.front();
q.pop();
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(i == j) continue;//不能倒给自己
node p1;
if(p.a[i] <= dx[j]-p.a[j])//能全部倒进去
{
p1.a[j]=p.a[i]+p.a[j];
p1.a[i]=0;
}
else//不能全部倒进去,有剩余
{
p1.a[i]=p.a[i]-(dx[j]-p.a[j]);
p1.a[j]=dx[j];
}
for(int k=0;k<3;k++)//将剩余的倒回来
{
if(k == i||k == j)
continue;
p1.a[k]=p.a[k];
}
p1.len=p.len;//记录倒过的次数
if(map[p1.a[0]][p1.a[1]][p1.a[2]] == 1)//判断是否倒过
continue;
map[p1.a[0]][p1.a[1]][p1.a[2]]=1;//倒过后做好标记
p1.len++;//次数加一
q.push(p1);
if((flag==p1.a[0]&&p1.a[0]==p1.a[1])||(flag==p1.a[0]&&p1.a[0]==p1.a[2])||(flag==p1.a[1]&&p1.a[1]==p1.a[2]))//判断可乐是否被平分
{
cout<<p1.len<<endl;
return;
}
}
}
cout<<"NO"<<endl;
}
int main()
{
int s,m,n;
while(scanf("%d %d %d",&s,&m,&n),s,m,n)
{
memset(map,0,sizeof(map));
dx[0]=s;
dx[1]=n;
dx[2]=m;
flag=s/2.0;
bfs(s,0,0);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/lmqpt/p/4691438.html