zoukankan      html  css  js  c++  java
  • 双向BFS统计

    Hdu1195

    两个四位密码 问你最少多少步能到达

    /*Huyyt*/
    #include<bits/stdc++.h>
    #define mem(a,b) memset(a,b,sizeof(a))
    #define pb push_back
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int mod = 1e9 + 7;
    const int gakki = 5 + 2 + 1 + 19880611 + 1e9;
    const int MAXN = 2e5 + 5, MAXM = 2e5 + 5;
    /*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], ed = 1;
    inline void addedge(int u, int v)
    {
            to[++ed] = v;
            nxt[ed] = Head[u];
            Head[u] = ed;
    }*/
    struct node
    {
            int now[5];
            int step;
    } startpoint, endpoint, cnt, to;
    void print(node x)
    {
            for (int i = 1; i <= 4; i++)
            {
                    cout << x.now[i];
            }
    }
    int finalans;
    bool flag = false;
    int vis[10][10][10][10];
    char s[10], e[10];
    queue<node> que[2];
    int stepsum[2];
    node get_change(node x, int aim)
    {
            node cur = x;
            swap(cur.now[aim], cur.now[aim + 1]);
            return cur;
    }
    node get_next(node x, int aim, int y)
    {
            x.now[aim] += y;
            if (x.now[aim] > 9)
            {
                    x.now[aim] = 1;
            }
            else if (x.now[aim] < 1)
            {
                    x.now[aim] = 9;
            }
            return x;
    }
    void get_vis(node x, int y)
    {
            vis[x.now[1]][x.now[2]][x.now[3]][x.now[4]] = y;
    }
    void check(node x, int y)
    {
            if (vis[x.now[1]][x.now[2]][x.now[3]][x.now[4]] == (y ^ 1))
            {
                    finalans = min(finalans, stepsum[0] + stepsum[1] + 1);
                    flag = true;
            }
            else if (vis[x.now[1]][x.now[2]][x.now[3]][x.now[4]] == -1)
            {
                    que[y].push(x);
                    get_vis(x, y);
            }
    }
    void doit(int x)
    {
            while (que[x].front().step == stepsum[x] && que[x].size())
            {
                    cnt = que[x].front();
                    que[x].pop();
                    for (int i = 1; i <= 4; i++)
                    {
                            if (i != 4)
                            {
                                    to = get_change(cnt, i);
                                    to.step = cnt.step + 1;
                                    check(to, x);
                            }
                            to = get_next(cnt, i, 1);
                            to.step = cnt.step + 1;
                            //cout << x << " ", print(cnt), cout << " ", print(to), cout << endl;
                            check(to, x);
                            to = get_next(cnt, i, -1);
                            to.step = cnt.step + 1;
                            //cout << x << " ", print(cnt), cout << " ", print(to), cout << endl;
                            check(to, x);
                    }
            }
            stepsum[x]++;
    }
    int main()
    {
            int T;
            scanf("%d", &T);
            while (T--)
            {
                    flag = false;
                    scanf("%s", s + 1), scanf("%s", e + 1);
                    stepsum[0] = stepsum[1] = 0;
                    for (int i = 1; i <= 9; i++)
                    {
                            for (int j = 1; j <= 9; j++)
                            {
                                    for (int k = 1; k <= 9; k++)
                                    {
                                            for (int w = 1; w <= 9; w++)
                                            {
                                                    vis[i][j][k][w] = -1;
                                            }
                                    }
                            }
                    }
                    finalans = INT_MAX;
                    for (int i = 0; i <= 1; i++)
                    {
                            while (que[i].size())
                            {
                                    que[i].pop();
                            }
                    }
                    startpoint.step = 0, endpoint.step = 0;
                    for (int i = 1; i <= 4; i++)
                    {
                            startpoint.now[i] = s[i] - '0';
                            endpoint.now[i] = e[i] - '0';
                    }
                    //print(startpoint), print(endpoint);
                    que[0].push(startpoint), que[1].push(endpoint);
                    get_vis(startpoint, 0), get_vis(endpoint, 1);
                    while (!flag)
                    {
                            if (que[0].size() > que[1].size())
                            {
                                    doit(1);
                            }
                            else
                            {
                                    doit(0);
                            }
                    }
                    printf("%d
    ", finalans);
            }
            return 0;
    }
    //Hdu1195

     Hdu1401

    8X8的棋盘上有四个棋子 问你能不能在八步之内把一个状态转移到另一一个状态

  • 相关阅读:
    WCF获取客户端IP,端口
    关闭应用程序(主程序)(WPF)
    WPF中自定义漂亮的进度条
    使用Silverlight4与Wcf4的Net.tcp实现的简单聊天室
    NET使用Js调用WebService
    [转]IBM服务器系列产品分类指南
    [转]delphi 变参函数:array of const
    [转]合并BPL包图文教程!
    [转]ABAP流程处理控制命令的说明
    [转]QR代码(Quick Response Code)简介
  • 原文地址:https://www.cnblogs.com/Aragaki/p/9495283.html
Copyright © 2011-2022 走看看