7 4 3 4 1 3 0 0 0
NO 3
这里操作如下:
for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(i==j)continue;//六种操作 if(now.num[i]<=rq[j]-now.num[j])//如果没有剩余,这里rq表示该容器的容积,num表示当前杯子内有的可乐的体积。 { nex.num[j]=now.num[j]+now.num[i]; nex.num[i]=0; } else//否则是有剩余的情况 { nex.num[i]=now.num[i]-(rq[j]-now.num[j]); nex.num[j]=rq[j]; } for(int ii=0;ii<3;ii++)//维护数据 { if(ii==i||ii==j) continue; nex.num[ii]=now.num[ii]; } nex.output=now.output+1; if(vis[nex.num[0]][nex.num[1]][nex.num[2]]==0)//广搜vis数组 { vis[nex.num[0]][nex.num[1]][nex.num[2]]=1; s.push(nex); } } }
if(now.num[0]==now.num[1]&&now.num[2]==0) { printf("%d\n",now.output); return ; } if(now.num[0]==now.num[2]&&now.num[1]==0) { printf("%d\n",now.output); return ; } if(now.num[1]==now.num[2]&&now.num[0]==0) { printf("%d\n",now.output); return ; }
#include<stdio.h> #include<queue> #include<string.h> using namespace std; struct qingkuang { int num[3]; int output; }now,nex; int vis[102][102][102]; int rq[3]; void bfs(int ss,int n,int m) { memset(vis,0,sizeof(vis)); queue<qingkuang>s; now.num[0]=ss; now.num[1]=n; now.num[2]=m; vis[ss][n][m]=1; now.output=0; s.push(now); while(!s.empty()) { now=s.front(); if(now.num[0]==now.num[1]&&now.num[2]==0) { printf("%d\n",now.output); return ; } if(now.num[0]==now.num[2]&&now.num[1]==0) { printf("%d\n",now.output); return ; } if(now.num[1]==now.num[2]&&now.num[0]==0) { printf("%d\n",now.output); return ; } s.pop(); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(i==j)continue; if(now.num[i]<=rq[j]-now.num[j]) { nex.num[j]=now.num[j]+now.num[i]; nex.num[i]=0; } else { nex.num[i]=now.num[i]-(rq[j]-now.num[j]); nex.num[j]=rq[j]; } for(int ii=0;ii<3;ii++) { if(ii==i||ii==j) continue; nex.num[ii]=now.num[ii]; } nex.output=now.output+1; if(vis[nex.num[0]][nex.num[1]][nex.num[2]]==0) { vis[nex.num[0]][nex.num[1]][nex.num[2]]=1; s.push(nex); } } } } printf("NO\n"); return ; } int main() { int s,n,m; while(~scanf("%d%d%d",&s,&n,&m)) { if(s==0&&n==0&&m==0)break; rq[0]=s; rq[1]=n; rq[2]=m; bfs(s,0,0); } }