• URAL 1016 Cube on the Walk


    URAL_1016

        由于立方体位于同一个点时可能有不同的形态,从而有不同的结果,因此我们可以根据立方体的形态将棋盘上一个点拆成若干个点,然后做最短路即可。当然,由于状态比较复杂,可以用哈希表映射出每个点的存储位置。

    #include<stdio.h>
    #include<string.h>
    #define MAXD 1000010
    #define Q 1000000
    #define HASH 1000003
    #define INF 0x3f3f3f3f
    int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, 1, -1};
    int ch[][6] = {{0, 1, 5, 2, 3, 4}, {0, 1, 3, 4, 5, 2}, {2, 4, 1, 3, 0, 5}, {4, 2, 0, 3, 1, 5}};
    char b[10];
    int sx, sy, tx, ty, qx[MAXD], qy[MAXD], st[MAXD][6], v[MAXD], where[MAXD];
    struct Hashmap
    {
        int f[MAXD], head[HASH], next[MAXD], e, state[MAXD][6], x[MAXD], y[MAXD], p[MAXD];
        void init()
        {
            memset(head, -1, sizeof(head));
            e = 1;
        }
        int hash(int s)
        {
            int i, h, seed = 131;
            h = qx[s] * seed + qy[s];
            for(i = 0; i < 6; i ++)
                h = h * seed + st[s][i];
            return (h & 0x7fffffff) % HASH;
        }
        int push(int s, int fa)
        {
            int i, h = hash(s);
            for(i = head[h]; i != -1; i = next[i])
                if(x[i] == qx[s] && y[i] == qy[s] && memcmp(st[s], state[i], sizeof(state[i])) == 0)
                    break;
            if(i == -1)
            {
                x[e] = qx[s], y[e] = qy[s], memcpy(state[e], st[s], sizeof(st[s])), f[e] = v[s];
                p[e] = fa;
                next[e] = head[h], head[h] = e;
                return e ++;
            }
            if(v[s] < f[i])
            {
                f[i] = v[s], p[i] = fa;
                return i;
            }
            return 0;
        }
    }hm;
    void print(int cur)
    {
        if(hm.p[cur] != -1)
            print(hm.p[cur]);
        printf(" %c%c", hm.x[cur] + 'a', hm.y[cur] + '1');
    }
    void solve()
    {
        int i, j, k, front, rear, x, y, newx, newy, ans = INF;
        front = 0, rear = 1;
        hm.init();
        hm.push(0, -1);
        while(front != rear)
        {
            x = qx[front], y = qy[front];
            for(i = 0; i < 4; i ++)
            {
                newx = x + dx[i], newy = y + dy[i];
                if(newx >= 0 && newx < 8 && newy >= 0 && newy < 8)
                {
                    qx[rear] = newx, qy[rear] = newy;
                    for(j = 0; j < 6; j ++)
                        st[rear][ch[i][j]] = st[front][j];
                    v[rear] = v[front] + st[rear][4];
                    if(k = hm.push(rear, where[front]))
                    {
                        where[rear] = k;
                        ++ rear;
                        if(rear > Q)
                            rear = 0;
                    }
                }
            }
            ++ front;
            if(front > Q)
                front = 0;
        }
        for(i = 1; i < hm.e; i ++)
            if(hm.x[i] == tx && hm.y[i] == ty && hm.f[i] < ans)
                ans = hm.f[i], k = i;
        printf("%d", ans);
        print(k);
        printf("\n");
    }
    int main()
    {
        int i;
        while(scanf("%s", b) == 1)
        {
            sx = b[0] - 'a', sy = b[1] - '1';
            scanf("%s", b);
            tx = b[0] - 'a', ty = b[1] - '1';
            qx[0] = sx, qy[0] = sy;
            for(i = 0; i < 6; i ++)
                scanf("%d", &st[0][i]);
            v[0] = st[0][4], where[0] = 1;
            solve();
        }
        return 0;
    }
  • 相关阅读:
    执行动态sql返回参数
    转: css box-sizing的用法
    使用mocMvc书写测试用例
    sprin-security之二(如何进行连接数据库)
    spring-security学习之(一)出入安全框架
    It's likely that neither a Result Type nor a Result Map was specified
    How To Install Java with Apt-Get on Ubuntu 16.04
    Kafka入门经典教程
    深入理解 Python 异步编程(上)
    ZooKeeper管理分布式环境中的数据
  • 原文地址:https://www.cnblogs.com/staginner/p/2477551.html
Copyright © 2020-2023  润新知