• P2730 魔板 Magic Squares


    题意:初始魔板1 2 3 4

           8 7 6 5

    三种操作

    “A”:交换上下两行;

    “B”:将最右边的一列插入最左边;

    “C”:魔板中央四格作顺时针旋转。

    下面是对基本状态进行操作的示范:

    A: 8 7 6 5

      1 2 3 4

    B: 4 1 2 3

      5 8 7 6

    C: 1 7 2 4

      8 6 3 5

    可以开一个结构体,里面实现ABC

    用队列套pair进行bfs

    first存魔板

    second用string存ans

    每次可以用string的加法addans

    判重? set!

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cctype>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<vector>
    #include<queue>
    using namespace std;
    #define DB double
    bool flag;
    int maxn=60;
    set<int>s;
    struct node
    {
        int xl[10];
        int &operator [] (int a)
        {
            return xl[a];
        }
        node()
        {
            memset(xl,0,sizeof xl);
        }
        void copy(const node &x)
        {
            for(int i=1;i<=8;i++)
                xl[i]=x.xl[i];
        }
        node A(const node &a)
        {
            node t=a;
            for(int i=1;i<=4;i++)
                swap(t.xl[i],t.xl[9-i]);
            return t;
        }
        node B(const node &a)
        {
            node t=a;
            int shang=t.xl[4];
            int xia=t.xl[5];
            for(int i=4;i>=2;i--)
                t.xl[i]=t.xl[i-1];
            t.xl[1]=shang;
            for(int i=5;i<=7;i++)
                t.xl[i]=t.xl[i+1];
            t.xl[8]=xia;
            return t;
        }
        node C(const node &aa)
        {
            node t=aa;
            int a=t.xl[2];
            int b=t.xl[3];
            int c=t.xl[6];
            int d=t.xl[7];
            t.xl[2]=d;
            t.xl[3]=a;
            t.xl[6]=b;
            t.xl[7]=c;
            return t;
        }
        bool pd(const node &b)
        {
            for(int i=1;i<=8;i++)
                if(xl[i]!=b.xl[i]) return false;
            return true;
        }
        int hash()
        {
            int x=0;
            for(int i=1;i<=8;i++)
                x=(x<<1)+(x<<3)+xl[i];
            return x;
        }
    }chu,goal;
    queue<pair<node,string> >q;
    inline int read()
    {
        int x=0,f=1;
        char ch=getchar();
        while(!isdigit(ch))
        {
            if(ch=='-')
                f=-f;
            ch=getchar();
        }
        while(isdigit(ch))
        {
            x=(x<<1)+(x<<3)+(ch^48);
            ch=getchar();
        }
        return x*f;
    }
    inline void put(int x)
    {
        if(x<0)
        {
            x=-x;
            putchar('-');
        }
        if(x>9)
            put(x/10);
        putchar(x%10+'0');
    }
    inline void bfs(node now)
    {
        q.push(make_pair(now,string()));
        s.insert(now.hash());
        while(!q.empty())
        {
            pair<node,string> tp=q.front();
            q.pop();
            if(tp.first.pd(goal))
            {
                cout<<tp.second.size()<<endl<<tp.second;
                exit(0);
            } 
            pair<node,string> a=make_pair(tp.first.A(tp.first),tp.second+'A');
            pair<node,string> b=make_pair(tp.first.B(tp.first),tp.second+'B');
            pair<node,string> c=make_pair(tp.first.C(tp.first),tp.second+'C');
            if(!s.count(a.first.hash()))
            {
                s.insert(a.first.hash());
                q.push(a);
            }
            if(!s.count(b.first.hash()))
            {
                s.insert(b.first.hash());
                q.push(b);
            }
            if(!s.count(c.first.hash()))
            {
                s.insert(c.first.hash());
                q.push(c);
            }
        }
    }
    int main()
    {
        for(int i=1;i<=8;i++)
        {
            goal[i]=read();
            chu[i]=i;
        }
        s.insert(chu.hash());
        bfs(chu);
        return 0;
    }
  • 相关阅读:
    RESTful API 设计指南
    浅析JS中的模块规范(CommonJS,AMD,CMD)
    Gitbucket—快速建立自己的Github
    单点登录详解
    Java常用类--处理日期
    Java常用类--数字常用类
    java常用类--字符串
    java常用类--系统相关
    java常用类--与用户互动
    设置PATH和CLASSPATH
  • 原文地址:https://www.cnblogs.com/olinr/p/9571234.html
Copyright © 2020-2023  润新知