#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
bool vis[103][103][103];
struct Status{
int s,n,m;
int step;
};
int s,n,m;
int half;
Status st,cur,temp;
bool Ok(Status s){
int num=0;
if(s.m==half) num++;
if(s.s==half) num++;
if(s.n==half) num++;
if(num==2) return true;
return false;
}
Status Oper(int a){
Status t=cur;
switch(a){
case 0: t.s>(n-t.n)?t.s-=(n-t.n),t.n=n:(t.n+=t.s,t.s=0);break;//S->N
case 1: t.n>(s-t.s)?t.n-=(s-t.s),t.s=s:(t.s+=t.n,t.n=0);break;//N->S
case 2: t.s>(m-t.m)?t.s-=(m-t.m),t.m=m:(t.m+=t.s,t.s=0);break;//S->M
case 3: t.m>(s-t.s)?t.m-=(s-t.s),t.s=s:(t.s+=t.m,t.m=0);break;//M->S
case 4:t.n>(m-t.m)?t.n-=(m-t.m),t.m=m:(t.m+=t.n,t.n=0);break;//N->M
case 5:t.m>(n-t.n)?t.m-=(n-t.n),t.n=n:(t.n+=t.m,t.m=0);break;//M->N
}
return t;
}
int bfs(){
queue<Status>q;
q.push(st);
while(!q.empty()){
cur=q.front();
q.pop();
if(Ok(cur)) return cur.step;
for(int i=0;i<6;i++){
temp=Oper(i);
if(!vis[temp.m][temp.n][temp.s]){
vis[temp.m][temp.n][temp.s]=1;
temp.step=cur.step+1;
q.push(temp);
// printf("%d:%d %d %d\n",temp.step,temp.s,temp.n,temp.m);
}//if
}//for
}//while
return -1;
}
int main(){
while(scanf("%d%d%d",&s,&n,&m),s){
if(s%2) {
printf("NO\n");
continue;
}//if
half=s/2;
st.m=0;st.n=0;st.s=s;st.step=0;
memset(vis,0,sizeof(vis));
int ans=bfs();
if(ans<0) printf("NO\n");
else printf("%d\n",ans);
}//while
return 0;
}
hdu1495 非常可乐(简单BFS)
最新推荐文章于 2021-07-28 16:25:15 发布