#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>

using namespace std;

const int MAXN = 101;

typedef struct {
    int c[3];
    int cnt;
}state;

queue<state> q;

int s[3];
bool vis[MAXN][MAXN][MAXN];

int dir[6][2] = { {0,1}, {1,0},
                  {0,2}, {2,0},
                  {1,2}, {2,1}
                };


bool ok(state x)
{
    if ( x.c[0] == x.c[1] && x.c[0] * 2 == s[0] )
        return true;
    if ( x.c[1] == x.c[2] && x.c[1] * 2 == s[0] )
        return true;
    if ( x.c[0] == x.c[2] && x.c[0] * 2 == s[0] )
        return true;
    return false;
}

void pour(state& x, int from, int to)
{
    if ( x.c[from] > 0 )
    {
        int amount = min(x.c[from], s[to] - x.c[to]);
        x.c[from] -= amount;
        x.c[to] += amount;
        x.cnt++;
    }
}

int BFS()
{
    if ( s[0] & 1 == 1 )
        return -1;
    while ( !q.empty() )
        q.pop();

    state first;
    first.c[0] = s[0];
    first.c[1] = 0;
    first.c[2] = 0;
    first.cnt = 0;
    vis[first.c[0]][first.c[1]][first.c[2]] = true;
    q.push(first);

    while ( !q.empty() )
    {
        state curr = q.front();
        q.pop();

        if ( ok(curr) )
            return curr.cnt;

        for (int i = 0; i < 6; i++)
        {
            state next = curr;
            pour(next, dir[i][0], dir[i][1]);
            if ( !vis[next.c[0]][next.c[1]][next.c[2]] )
            {
                vis[next.c[0]][next.c[1]][next.c[2]] = true;
                q.push(next);
            }

        }
    }

    return -1;
}

int main()
{
//    freopen("1.txt","r",stdin);
    while ( scanf("%d%d%d", &s[0], &s[1], &s[2]) == 3 )
    {
        if ( s[0] == 0 && s[1] == 0 && s[2] == 0 )
            break;
        memset(vis, 0, sizeof(vis));

        int ans = BFS();

        if ( ans == -1 )
            printf("NO\n");
        else
            printf("%d\n",ans);

    }

    return 0;
}

 

posted on 2013-09-17 20:17  Sinker  阅读(223)  评论(0编辑  收藏  举报