码迷,mamicode.com
首页 > 其他好文 > 详细

P2730 魔板 Magic Squares

时间:2018-09-01 20:31:34      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:flag   col   algo   type   hash   bool   return   stream   algorithm   

题意:初始魔板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 olinr return
#define _ 0
#define love_nmr 0
#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);
    olinr ~~(0^_^0)+love_nmr;
}

 

P2730 魔板 Magic Squares

标签:flag   col   algo   type   hash   bool   return   stream   algorithm   

原文地址:https://www.cnblogs.com/olinr/p/9571234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!