非常可乐 HDU - 1495

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 10
using namespace std;
int s,n,m;
struct node
{
int k1,k2,k3;//三个容器
int step;//步数
};
node now,net;
int vis[105][105][105];//记录是否这样倒过可乐
void bfs()
{
int f=0;
queueq;
now.k1=s,now.k2=0,now.k3=0,now.step=0;//初始化,第一个容器为s,其他两个为空,步数为0
vis[s][0][0]=1;//标记
q.push(now);//入队
while(!q.empty())
{
now=q.front();
q.pop();
if(now.k1now.k2&&now.k30)//k1等于k2且k3等于0,即可乐全被分完
{
f=1;
cout< break;
}
for(int i=1; i<=6; i++)//六种倒可乐的方法
{
if(i1)//(1倒入2) 后面的代码都一样,就是换了一个容器,思想一样
{
if(now.k1+now.k2<=n)//如果不能够装满n
{
net.k1=0;
net.k2=now.k1+now.k2;
net.k3=now.k3;
}
else//可以装满
{
net.k1=now.k1+now.k2-n;
net.k2=n;
net.k3=now.k3;
}
}
if(i
2)//(1倒入3)
{
if(now.k1+now.k3<=m)//如果不能够装满m
{
net.k1=0;
net.k3=now.k1+now.k3;
net.k2=now.k2;
}
else
{
net.k1=now.k1+now.k3-m;
net.k3=m;
net.k2=now.k2;
}
}
if(i3)//(2倒入1)
{
if(now.k1+now.k2<=s)//如果不能够装满s
{
net.k1=now.k1+now.k2;
net.k2=0;
net.k3=now.k3;
}
else
{
net.k1=s;
net.k2=now.k1+now.k2-s;
net.k3=now.k3;
}
}
if(i
4)//(2倒入3)
{
if(now.k2+now.k3<=m)//如果不能够装满m
{
net.k2=0;
net.k3=now.k2+now.k3;
net.k1=now.k1;
}
else
{
net.k2=now.k2+now.k3-m;
net.k3=m;
net.k1=now.k1;
}
}
if(i5)//(3倒入1)
{
if(now.k1+now.k3<=s)//如果不能够装满s
{
net.k1=now.k1+now.k3;
net.k3=0;
net.k2=now.k2;
}
else
{
net.k1=s;
net.k3=now.k1+now.k3-s;
net.k2=now.k2;
}
}
if(i
6)//(3倒入2)
{
if(now.k3+now.k2<=n)//如果不能够装满n
{
net.k3=0;
net.k2=now.k2+now.k3;
net.k1=now.k1;
}
else
{
net.k3=now.k2+now.k3-n;
net.k2=n;
net.k1=now.k1;
}
}
if(!vis[net.k1][net.k2][net.k3])//这样的装法没出现过
{
net.step=now.step+1;
vis[net.k1][net.k2][net.k3]=1;//标记
q.push(net);

        }
    }
}
if(f==0)
    cout<<"NO"<

}
int main()
{
while(cin>>s>>n>>m,s+n+m)
{
if(n {
int t=n;
n=m;
m=t;
}
MEM(vis,0);
if(s%2!=0)//奇数肯定不能平分
cout<<“NO”< else
bfs();
}
}

你可能感兴趣的:(集训搜索)