魔板 Magic Squares(bfs优化)

[USACO3.2]魔板 Magic Squares

题目背景

在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板。这是一张有8个大小相同的格子的魔板:

1 2 3 4

8 7 6 5

题目描述

我们知道魔板的每一个方格都有一种颜色。这8种颜色用前8个正整数来表示。可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列。对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示。这是基本状态。

这里提供三种基本操作,分别用大写字母“A”,“B”,“C”来表示(可以通过这些操作改变魔板的状态):

“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

对于每种可能的状态,这三种基本操作都可以使用。

你要编程计算用最少的基本操作完成基本状态到目标状态的转换,输出基本操作序列。

输入格式

只有一行,包括8个整数,用空格分开(这些整数在范围 1——8 之间)不换行,表示目标状态。

输出格式

Line 1: 包括一个整数,表示最短操作序列的长度。

Line 2: 在字典序中最早出现的操作序列,用字符串表示,除最后一行外,每行输出60个字符。

样例 #1

样例输入 #1

2 6 8 4 5 7 3 1

样例输出 #1

7 
BCABCCB

提示

题目翻译来自NOCOW。

USACO Training Section 3.2

思路

bfs优化。为了避免重复搜索超时,使用set优化,使出现过的不再搜第二次。也算是剪枝吧。

#include <bits/stdc++.h>
typedef long double ld;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define mt make_tuple
#define eb emplace_back
#define pob pop_back
#define rz resize
#define mem(a,b) memset(a,b,sizeof(a))
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define debug(a) cout<<#a<<"="<<a<<endl;
#define xx first
#define yy second
#define qwe(i,a,b) for(int i=a;i<=b;i++)
#define ewq(i,a,b) for(int i=a;i>=b;i--)
inline ll rr(){ll f=1,x=0;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');return f*x;}
using namespace std;
// ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const ll INF=0x3f3f3f3f,inf=0x3f3f3f3f3f3f3f;
const ll mod[2]={int(1e9 + 7), 10007};
const int base[2]={29,31};
const int maxn=1e2+6;
ll qpow(ll x,ll n){ll ans=1;while(n>0){if(n&1)ans=ans*x%mod[1];x=x*x%mod[1];n>>=1;}return ans;}

int aa[maxn];
set<string> se;
struct node {
    int t[maxn],st;
    string c;
};
bool check(int *s) {
    for(int i=0;i<8;i++) {
        if(s[i]!=aa[i]) return 0;
    }
    return 1;
}
string make_it_string(int *s) {
    string to;
    for(int i=0;i<8;i++) {
        to=to+char(s[i]+'0');
    }
    return to;
}
int main(int argc, char const *argv[]) {
    for(int i=0;i<4;i++) {
        aa[i]=rr();
    }
    for(int i=7;i>=4;i--) {
        aa[i]=rr();
    }
    queue<node> q;
    node n;
    for(int i=0;i<4;i++) {
        n.t[i]=i+1;
    }
    n.t[4]=8,n.t[5]=7,n.t[6]=6,n.t[7]=5;
    n.st=0,n.c="";
    q.push(n);
    while (!q.empty()) {
        node tmp=q.front();
        q.pop();
        string k=make_it_string(tmp.t);
        if(se.count(k)) continue;
        se.insert(k);
        if(check(tmp.t)) {
            std::cout << tmp.st << '\n';
            std::cout << tmp.c << '\n';
            return 0;
        }
        node now;
        // 第一种变换
        for(int i=4;i<8;i++) {
            now.t[i-4]=tmp.t[i];
        }
        for(int i=4;i<8;i++) {
            now.t[i]=tmp.t[i-4];
        }
        now.st=tmp.st+1;
        now.c=tmp.c+"A";
        q.push(now);

        // 第二种变换
        for(int i=0;i<3;i++) {
            now.t[i+1]=tmp.t[i];
        }
        for(int i=4;i<7;i++) {
            now.t[i+1]=tmp.t[i];
        }
        now.t[0]=tmp.t[3],now.t[4]=tmp.t[7];
        now.st=tmp.st+1;
        now.c=tmp.c+"B";
        q.push(now);
    
        // 第三种变换
        int rem=tmp.t[1];
        tmp.t[1]=tmp.t[5];
        tmp.t[5]=tmp.t[6];
        tmp.t[6]=tmp.t[2];
        tmp.t[2]=rem;
        for(int i=0;i<8;i++) now.t[i]=tmp.t[i];
        now.st=tmp.st+1;
        now.c=tmp.c+"C";
        q.push(now);
    }
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值