标签:des style java color os strong io for
7 4 3 4 1 3 0 0 0
NO 3
—————————————————————分割线—————————————————————————————
有a,b,c三个杯子,倒来倒去,那么总共有6种情况
1、把a倒入b
2、把a倒入c
3、把b倒入a
4、把b倒入c
5、把c倒入a
6、把c倒入b
每种情况还有判断 要倒的杯子还剩多少能装,bfs走起
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
#define M 105
using namespace std;
int vis[M][M][M],n,m,s,ans,dx[3];
struct node
{
int x[3],step;
};
bool check(int a,int b,int c)
{
if((a==b&&a==s/2)||(a==c&&a==s/2)||(b==c&&b==s/2)) return true;
return false;
}
int bfs()
{
memset(vis,0,sizeof vis);
queue<node> q;
node a,b;
a.x[0]=s,a.x[1]=0,a.x[2]=0,a.step=0;
vis[s][0][0]=1;
q.push(a);
while(!q.empty()){
a=q.front();q.pop();
for(int i=0;i<3;++i){
for(int j=0;j<3;++j){
if(j==i) continue;
int u=dx[j]-a.x[j];
if(a.x[i]>u&&u>0){
b.x[i]=a.x[i]-u;
b.x[j]=dx[j];
}
else{
b.x[i]=0;
b.x[j]=a.x[j]+a.x[i];
}
for(int k=0;k<3;++k){
if(k==i||k==j) continue;
b.x[k]=a.x[k];
}
if(!vis[b.x[0]][b.x[1]][b.x[2]]){
b.step=a.step+1;
vis[b.x[0]][b.x[1]][b.x[2]]=1;
if(check(b.x[0],b.x[1],b.x[2])){ //判断语句一定要写这里,不能写出队那里,以贡献好几个MLE,因为///每次有6个状态
ans=b.step;
return 1;
}
q.push(b);
}
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d%d",&s,&n,&m)!=EOF&&(s+n+m)!=0){
if(s%2){
printf("NO\n");
continue;
}
else{
dx[0]=s,dx[1]=n,dx[2]=m;
if(bfs()){
cout<<ans<<endl;
}
else cout<<"NO"<<endl;
}
}
return 0;
}
HDU 1495——非常可乐( BFS ),布布扣,bubuko.com
标签:des style java color os strong io for
原文地址:http://blog.csdn.net/u014141559/article/details/38239223