hdu 1495——非常可樂

來源:互聯網
上載者:User

記憶化搜尋+寬搜

#include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;struct Node{int a,b,c;int step;};int a,b,c;int dp[105][105][105];int flag;int min(int a,int b){return a<b?a:b;}bool Judge(Node &x){int flag=0;if(x.a==a/2)flag++;if(x.b==a/2)flag++;if(x.c==a/2)flag++;if(flag==2)return 1;return 0;}void Put(Node& x,int i){if(i==1)//a b{int temp=min(x.a,b-x.b);x.a-=temp;x.b+=temp;}if(i==2)//a c{int temp=min(x.a,c-x.c);x.a-=temp;x.c+=temp;}if(i==3)//b a{int temp=min(x.b,a-x.a);x.b-=temp;x.a+=temp;}if(i==4)//b c{int temp=min(x.b,c-x.c);x.b-=temp;x.c+=temp;}if(i==5)//c a{int temp=min(x.c,a-x.a);x.c-=temp;x.a+=temp;}if(i==6)//c b{int temp=min(x.c,b-x.b);x.c-=temp;x.b+=temp;}}void bfs(){Node start,temp1,temp2;int i;memset(dp,0,sizeof(dp));queue<Node> q;start.a=a;start.b=start.c=start.step=0;q.push(start);while(!q.empty()){temp1=q.front();q.pop();if(Judge(temp1)){flag=1;cout<<temp1.step<<endl;return;}for(i=1;i<=8;i++){temp2=temp1;Put(temp2,i);if(!dp[temp2.a][temp2.b][temp2.c]){dp[temp2.a][temp2.b][temp2.c]=1;temp2.step=temp1.step+1;q.push(temp2);}}}}int main(){while(cin>>a>>b>>c&&a+b+c){flag=0;if(a%2==1){cout<<"NO"<<endl;continue;}bfs();if(flag==0)cout<<"NO"<<endl;}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.