zoukankan      html  css  js  c++  java
  • HDU 1667 The Rotation Game

    HDU_1667

        如果我们确定要移动一类整数的话,那么其他两类整数实际上都可以看作是0,因此可以固定一个中间八个整数为1、其余整数为0的终态,然后预处理出所有可能的状态,在查询的时候只要枚举1、2、3分别作为要移动到中间整数就可以计算出最优解了。

    #include<stdio.h>
    #include<string.h>
    #define HASH 1000007
    #define MAXD 1000010
    #define INF 0x3f3f3f3f
    int op[][24] =
    {
        {22, 1, 0, 3, 4, 5, 2, 7, 8, 9, 10, 6, 12, 13, 14, 11, 16, 17, 18, 19, 15, 21, 20, 23},
        {0, 23, 2, 1, 4, 5, 6, 7, 3, 9, 10, 11, 8, 13, 14, 15, 16, 12, 18, 19, 20, 17, 22, 21},
        {0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 4, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23},
        {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 13, 20, 21, 22, 23},
        {0, 3, 2, 8, 4, 5, 6, 7, 12, 9, 10, 11, 17, 13, 14, 15, 16, 21, 18, 19, 20, 23, 22, 1},
        {2, 1, 6, 3, 4, 5, 11, 7, 8, 9, 10, 15, 12, 13, 14, 20, 16, 17, 18, 19, 22, 21, 0, 23},
        {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 19, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23},
        {0, 1, 2, 3, 10, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23},
    };
    int ini[] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0};
    struct HashMap
    {
        int head[HASH], size, next[MAXD], st[MAXD];
        void init()
        {
            memset(head, -1, sizeof(head)), size = 0;
        }
        int find(int _st)
        {
            int i, h = _st % HASH;
            for(i = head[h]; i != -1; i = next[i])
                if(st[i] == _st) break;
            return i;
        }
        void push(int _st)
        {
            int h = _st % HASH;
            st[size] = _st;
            next[size] = head[h], head[h] = size ++;
        }
    }hm;
    int pre[MAXD], type[MAXD], dis[MAXD], q[MAXD];
    int code[25], g[25];
    char list[MAXD], b[MAXD];
    int encode(int *code)
    {
        int i, ans = 0;
        for(i = 0; i < 24; i ++) ans = ans << 1 | code[i];
        return ans;
    }
    void decode(int st)
    {
        int i;
        for(i = 23; i >= 0; i --) code[i] = st & 1, st >>= 1;
    }
    void prepare()
    {
        int i, j, k, x, y, rear = 0, t[25];
        hm.init();
        x = encode(ini);
        pre[0] = -1, dis[0] = 0;
        hm.push(x), q[rear ++] = x;
        for(i = 0; i < rear; i ++)
        {
            x = q[i];
            decode(x);
            for(j = 0; j < 8; j ++)
            {
                for(k = 0; k < 24; k ++) t[k] = code[op[j][k]];
                y = encode(t);
                k = hm.find(y);
                if(k == -1)
                {
                    pre[rear] = i, dis[rear] = dis[i] + 1, type[rear]= j;
                    hm.push(y), q[rear ++] = y;
                }
                else if(dis[i] + 1 == dis[k] && j < type[k])
                    pre[k] = i, type[k] = j;
            }
        }
    }
    void print(int cur, char *q, int rear)
    {
        if(pre[cur] == -1) return;
        q[rear] = type[cur] + 'A';
        print(pre[cur], q, rear + 1);
    }
    void solve()
    {
        int i, j, t[25], ans = INF, cur, mid;
        for(i = 1; i < 24; i ++) scanf("%d", &g[i]);
        for(i = 1; i <= 3; i ++)
        {
            for(j = 0; j < 24; j ++) t[j] = g[j] == i;
            j = hm.find(encode(t));
            if(j != -1)
            {
                if(dis[j] < ans)
                    ans = dis[j], cur = j, mid = i, print(cur, list, 0), list[ans] = '\0';
                else if(dis[j] == ans)
                {
                    print(j, b, 0), b[ans] = '\0';
                    if(strcmp(b, list) == -1)
                        strcpy(list, b), cur = j, mid = i;
                }
            }
        }
        if(ans == 0) printf("No moves needed\n%d\n", mid);
        else printf("%s\n%d\n", list, mid);
    }
    int main()
    {
        int t;
        prepare();
        while(scanf("%d", &t), t)
        {
            g[0] = t;
            solve();
        }
        return 0;
    }
  • 相关阅读:
    Codeforces 834D The Bakery
    hdu 1394 Minimum Inversion Number
    Codeforces 837E Vasya's Function
    Codeforces 837D Round Subset
    Codeforces 825E Minimal Labels
    Codeforces 437D The Child and Zoo
    Codeforces 822D My pretty girl Noora
    Codeforces 799D Field expansion
    Codeforces 438D The Child and Sequence
    Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D)
  • 原文地址:https://www.cnblogs.com/staginner/p/2661115.html
Copyright © 2011-2022 走看看