• poj3221


    bfs

    View Code
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    using namespace std;

    struct Edge
    {
    int v, next;
    }edge[50];

    struct Node
    {
    int f[7];
    int s;
    }q[1000000], node;

    int head[10];
    int ecount = 0;
    bool vis[1000000];
    int step[1000000];

    void addedge(int a, int b)
    {
    edge[ecount].v = b;
    edge[ecount].next = head[a];
    head[a] = ecount++;
    }

    int hash(Node &a)
    {
    int x = 1;
    int ret = 0;
    for (int i = 0; i < 7; i++)
    {
    ret += a.f[i] * x;
    x *= 7;
    }
    return ret;
    }

    void bfs()
    {
    int front = 0;
    int rear = 1;
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < 7; i++)
    q[0].f[i] = i;
    int h = hash(q[0]);
    step[h] = q[0].s = 0;
    vis[h] = true;
    while (front < rear)
    {
    int a;
    for (int i = 0; i < 7; i++)
    if (q[front].f[i] == 0)
    {
    a = i;
    break;
    }
    for (int i = head[a]; i != -1; i = edge[i].next)
    {
    int b = edge[i].v;
    swap(q[front].f[a], q[front].f[b]);
    h = hash(q[front]);
    if (!vis[h])
    {
    vis[h] = true;
    q[rear] = q[front];
    step[h] = q[rear++].s = q[front].s + 1;
    }
    swap(q[front].f[a], q[front].f[b]);
    }
    front++;
    }
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    memset(head, -1, sizeof(head));
    memset(step, -1, sizeof(step));
    addedge(0, 2);
    addedge(0, 4);
    addedge(0, 6);
    addedge(1, 6);
    addedge(1, 2);
    addedge(2, 1);
    addedge(2, 0);
    addedge(2, 3);
    addedge(3, 2);
    addedge(3, 4);
    addedge(4, 3);
    addedge(4, 0);
    addedge(4, 5);
    addedge(5, 4);
    addedge(5, 6);
    addedge(6, 1);
    addedge(6, 0);
    addedge(6, 5);
    bfs();
    int t;
    scanf("%d", &t);
    getchar();
    while (t--)
    {
    for (int i = 0; i < 7; i++)
    {
    char ch = getchar();
    node.f[i] = ch - '0';
    }
    getchar();
    printf("%d\n", step[hash(node)]);
    }
    return 0;
    }

  • 相关阅读:
    【搜索】棋盘 luogu-3956
    【动态规划】石子合并 luogu-1880
    【动态规划】合唱队形 luogu-
    【模拟】报名签到 luogu-4445
    【排序+贪心】导弹拦截 luogu-1158
    【模拟】不高兴的津津
    【模拟】选数 luogu-1037
    「JOISC2020」建筑装饰 4
    「清华集训」小 Y 和恐怖的奴隶主
    「CF708E」Student's Camp
  • 原文地址:https://www.cnblogs.com/rainydays/p/2210574.html
Copyright © 2020-2023  润新知