• HDU 1043 Eight BFS


    题意:就是恢复成1,2,3,4,5,6,7,8,0;

    分析:暴力BFS预处理,所有路径,用康拓展开判重,O(1)打印 93ms 还是很快的

    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    using namespace std;
    const int N=5005;
    int fac[]= {1,1,2,6,24,120,720,5040,40320,362880};
    int aim;
    int cantor(char s[])
    {
        int ans=0;
        for(int i=1,j=8; i<=9; ++i,--j)
        {
            int tmp=0;
            for(int k=i+1; k<=9; ++k)
                if(s[i]>s[k])++tmp;
            ans+=(tmp*fac[j]);
        }
        return ans;
    }
    struct Node
    {
        char s[10];
        int hs;
    };
    struct asd
    {
       bool vis;
       char c;
       int pre;
    }o[362880];
    queue<Node>q;
    void bfs()
    {
        Node a;
        for(int i=1; i<=8; ++i)
            a.s[i]='0'+i;
        a.s[9]='0';
        aim=a.hs=cantor(a.s);
        o[aim].vis=1;
        q.push(a);
        while(!q.empty())
        {
            a=q.front();
            q.pop();
            int now=a.hs;
            int x;
            for(int i=1; i<=9; ++i)
                if(a.s[i]=='0')x=i;
            if(x-3>0)
            {
                bool flag=0;
                swap(a.s[x],a.s[x-3]);
                a.hs=cantor(a.s);
               if(o[a.hs].vis)
                    flag=1;
                if(!flag)
                {
                    o[a.hs].vis=1;
                    o[a.hs].c='d';
                    o[a.hs].pre=now;
                    q.push(a);
                }
                swap(a.s[x],a.s[x-3]);
            }
            if(x+3<10)
            {
                bool flag=0;
                swap(a.s[x],a.s[x+3]);
                a.hs=cantor(a.s);
                if(o[a.hs].vis)
                    flag=1;
                if(!flag)
                {
                    o[a.hs].vis=1;
                    o[a.hs].c='u';
                    o[a.hs].pre=now;
                    q.push(a);
                }
                swap(a.s[x],a.s[x+3]);
            }
            if(x%3!=1)
            {
                bool flag=0;
                swap(a.s[x],a.s[x-1]);
                a.hs=cantor(a.s);
                if(o[a.hs].vis)
                    flag=1;
                if(!flag)
                {
                    o[a.hs].vis=1;
                    o[a.hs].c='r';
                    o[a.hs].pre=now;
                    q.push(a);
                }
                swap(a.s[x],a.s[x-1]);
            }
            if(x%3)
            {
                bool flag=0;
                swap(a.s[x],a.s[x+1]);
                a.hs=cantor(a.s);
               if(o[a.hs].vis)
                    flag=1;
                if(!flag)
                {
                    o[a.hs].vis=1;
                    o[a.hs].c='l';
                    o[a.hs].pre=now;
                    q.push(a);
                }
                swap(a.s[x],a.s[x+1]);
            }
        }
    }
    char str[30],tmp[10];
    void print(int u)
    {
        while(u!=aim)
        {
           printf("%c",o[u].c);
           u=o[u].pre;
        }
        printf("
    ");
    }
    int main()
    {
        for(int i=0;i<326880;++i)
          o[i].vis=0;
        bfs();
        while(gets(str))
        {
            int l=0;
            for(int i=0; str[i]!='0'; ++i)
            {
                if(str[i]=='x')tmp[++l]='0';
                else if(str[i]>='1'&&str[i]<='9')tmp[++l]=str[i];
            }
            int ans=cantor(tmp);
            if(!o[ans].vis)
                printf("unsolvable
    ");
            else
                print(ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    (转)expfilt 命令
    (转)第二十三节 inotify事件监控工具
    数据结构之平衡二叉树(AVL)
    安装apache2.4.10
    centos下编译安装mysql5.6
    随机 I/O & 顺序 I/O
    什么是mysql中的元数据
    linux中mail函数不能发送邮件怎么办
    检测MYSQL不同步发邮件通知的脚本
    mysql自动备份策略
  • 原文地址:https://www.cnblogs.com/shuguangzw/p/5248035.html
Copyright © 2020-2023  润新知