HDU 1495 非常可乐

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1495

#include <iostream>
using namespace std;
int S,N,M;
int vis[110][110]={0};
        int ans ;
int min(const int a,const int b)
{
    return a<b?a:b;
}
void DFS(int d,int A,int B,int C)
{
    if(vis[A][B] && d > vis[A][B]) 
    {
        return ;
    }
    vis[A][B] = d;
    if( A ==S/2 && (A==B||A==C) || (C == S/2 && B==C))
    {
        ans = min(ans,d);
    }
    if( A > 0 )
    {
        if(A+B<=N) DFS(d+1,0,A+B,C);
        else DFS(d+1,A-N+B,N,C); 
        if(A+C<=M) DFS(d+1,0,B,A+C);
        else DFS(d+1,A-M+C,B,M);
    }
    if( B > 0)
    {
        if(B+A<=S) DFS(d+1,B+A,0,C);
        if(B+C<=M) DFS(d+1,A,0,B+C);
        else DFS(d+1,A,B-M+C,M);
    }
    if( C > 0)
    {
        if(C+A<=S) DFS(d+1,C+A,B,0);
        if(C+B<=N) DFS(d+1,A,C+B,0);
        else DFS(d+1,A,N,C-N+B);
    }
}
int main(int argc, const char *argv[])
{
    while(cin>>S>>N>>M && (S+N+M)>0)
    {
        if(N<S/2&&M<S/2 || S%2) 
        {
            cout<<"NO"<<endl;
            continue;
        }
        memset(vis,0,sizeof(vis));
        ans = 1<<30;
        DFS(0,S,0,0);
        if(ans==1<<30)
            cout<<"NO"<<endl;
        else cout<<ans<<endl;
    }
    return 0 ;
}

 

posted @ 2013-09-22 21:02  Destino74  阅读(213)  评论(0编辑  收藏  举报