Medium HDU 1495 very coke, BFS.

Source: Internet
Author: User

Description

It is a pleasant thing to drink coke after exercise, but Seeyou doesn't think so. Because every time when Seeyou bought Coke, Ox asked to share this bottle of Coke with seeyou, and must drink as much as seeyou. But Seeyou's hands only two cups, their capacity is N ml and M ml cola volume for S (s<101) ml (just fill a bottle), they can pour coke between three each other (there is no scale, and s==n+m,101>s>0,n>0,m>0). Smart Acmer, do you think they can split it?   If you can output the minimum number of Coke, if you cannot output "no". Direct BFS is good, 100*100*100 state, or not super ... (Is there a better way?) ) code is as follows:
#include <iostream>#include<cstring>using namespacestd;intque[1000006],las,fir;inta,b,c;Long Longvis[101][101][101];BOOLJudgeintXintYintz) {    if(vis[x][y][z]==-1)        return 1; return 0;}Long LongSlove () {intTemp,t1,t2,t3; int Base; Las=fir=0; memset (Vis,-1,sizeof(VIS)); Que[las++]=a* the* the+0* the+0; vis[a][0][0]=0;  while(las-fir) {Temp=que[fir++]; T1=temp/( the* the); T2= (temp/ the)% the; T3=temp% the; Base=VIS[T1][T2][T3]; if((t1==a/2&& (t2==a/2|| t3==a/2))|| (t2==a/2&&t3==a/2))            return Base; Temp=min (t1,b-T2); T1-=temp; T2+=temp; if(judge (T1,t2,t3)) {VIS[T1][T2][T3]=Base+1; Que[las++]=t1* ( the* the) +t2* the+T3; } T1+=temp; T2-=temp; Temp=min (t1,c-T3); T1-=temp; T3+=temp; if(judge (T1,t2,t3)) {VIS[T1][T2][T3]=Base+1; Que[las++]=t1* ( the* the) +t2* the+T3; } T1+=temp; T3-=temp; Temp=min (t2,a-t1); T2-=temp; T1+=temp; if(judge (T1,t2,t3)) {VIS[T1][T2][T3]=Base+1; Que[las++]=t1* ( the* the) +t2* the+T3; } T2+=temp; T1-=temp; Temp=min (t2,c-T3); T2-=temp; T3+=temp; if(judge (T1,t2,t3)) {VIS[T1][T2][T3]=Base+1; Que[las++]=t1* ( the* the) +t2* the+T3; } T2+=temp; T3-=temp; Temp=min (t3,a-t1); T3-=temp; T1+=temp; if(judge (T1,t2,t3)) {VIS[T1][T2][T3]=Base+1; Que[las++]=t1* ( the* the) +t2* the+T3; } T3+=temp; T1-=temp; Temp=min (t3,b-T2); T3-=temp; T2+=temp; if(judge (T1,t2,t3)) {VIS[T1][T2][T3]=Base+1; Que[las++]=t1* ( the* the) +t2* the+T3; } T3+=temp; T2-=temp; }    return-1;}intMain () {Ios::sync_with_stdio (false); Long Longans;  for(cin>>a>>b>>c; A+b+c;cin>>a>>b>>C) {if(A%2) ans=-1; Elseans=Slove (); if(ans!=-1) cout<<ans<<Endl; Elsecout<<"no\n"; }    return 0;}
View Code

Medium HDU 1495 very coke, BFS.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.