zoukankan      html  css  js  c++  java
  • 九度1457...

    #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
    ");
            else
                printf("%d
    ",ans);
    
        }
    
        return 0;
    }
  • 相关阅读:
    AJAX异步交互
    Java 异常讲解(转)
    Spring 配置文件详解 (以2.5为例)
    Java 获取当前系统时间方法比较
    Cannot change version of project facet Dynamic web module to 3.0
    使用 GCC 调试程序
    汇编 内存段划分和寄存器
    java.lang.StringBuilder
    java.lang.String
    建立和断开与MySQL服务器的连接
  • 原文地址:https://www.cnblogs.com/jiongjiong-mengmeng/p/3327102.html
Copyright © 2011-2022 走看看