zoukankan      html  css  js  c++  java
  • 洛谷P2730 [IOI]魔板 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
    2 6 8 4 5 7 3 1 
    输出 #1
    7 
    BCABCCB

    说明/提示

    题目翻译来自NOCOW。

    USACO Training Section 3.2

    解析:-----BFS宽搜-----

    对于状态采用了字符串的存储是采用了将八个数字压成一个字符串的方式
    例如初始状态为"12345678",而字符串存储为"12348765" 。
    然后根据三个变换规则ABC进行变换
    直到变成了目标状态
    注意目标状态也要第二部分翻转
    例如"26845731",存储为"26841375"。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<string>
     6 #include<algorithm>
     7 #include<iomanip>
     8 #include<cstdlib>
     9 #include<queue>
    10 #include<set>
    11 #include<map>
    12 #include<stack>
    13 #include<vector>
    14 #define LL long long
    15 #define re register
    16 #define Max 100001
    17 struct MoBan {
    18     std::string p;
    19     std::string str;
    20     int step;
    21 };
    22 std::queue<MoBan>q;
    23 std::string D;
    24 int ans;
    25 std::map<std::string,int>m;
    26 std::string BFS()
    27 {
    28     MoBan now,net;
    29     while(!q.empty()) {
    30         now=q.front();q.pop();
    31         std::string str=now.str;
    32         int t=now.step;
    33         std::string p=now.p;
    34         if(str==D) {
    35             ans=t;
    36             return p;
    37             break;
    38         }
    39         ++t;
    40         //A
    41         std::string d="";
    42         for(re int i = 4 ; i <= 7 ; ++ i) d+=str[i];
    43         for(re int i = 0 ; i <= 3 ; ++ i) d+=str[i];
    44         net.str=d;net.p=p+"A";
    45         net.step=t;
    46         if(m[d]!=1) q.push(net),m[d]=1;
    47         //B
    48         std::string a="";
    49         a+=str[3];
    50         for(re int i = 0 ; i < 3 ; ++ i) a+=str[i];
    51         a+=str[7];
    52         for(re int i = 4 ; i < 7 ; ++ i) a+=str[i];
    53         net.str=a;net.p=p+"B";
    54         if(m[a]!=1) q.push(net),m[a]=1;
    55         //C
    56         std::string c="";
    57         c+=str[0],c+=str[5],c+=str[1],c+=str[3],c+=str[4],c+=str[6],c+=str[2],c+=str[7];
    58         net.str=c;net.p=p+"C";
    59         if(m[c]!=1) q.push(net),m[c]=1;
    60     }
    61 }
    62 int main()
    63 {
    64     char ch[9];std::string str="12348765";
    65     for(re int i = 1 ; i <= 8 ; ++ i) std::cin >> ch[i];
    66     for(re int i = 1 ; i <= 4 ; ++ i) D+=ch[i];
    67     for(re int i = 8 ; i >= 5 ; -- i) D+=ch[i];
    68     MoBan now;m[str]=1;
    69     now.p="";now.step=0;now.str=str;q.push(now);std::string p=BFS();
    70     printf("%d
    ",ans);
    71     int len=p.length();std::cout << p[0];
    72     for(re int i = 1 ; i < len ; ++ i) {
    73         std::cout << p[i];
    74         if(i%60==0) std::cout << '
    ';
    75     }
    76     return 0;
    77 }
    AC代码
  • 相关阅读:
    Win10系统的开机启动项如何去关闭?
    如何对Win10电脑文件夹选项进行设置?
    DNS缓存失败怎么解决?
    如何关闭Win10系统的时间轴功能?
    怎么处理Win7电脑打开软件速度慢的情况?
    Win10带有网络连接的安全模式怎么开启?
    如何解决Win10电脑网速慢的问题?
    【Beta】Scrum Meeting 5
    【Beta】Scrum Meeting 4
    【Beta】Scrum Meeting 3
  • 原文地址:https://www.cnblogs.com/ypay/p/11287637.html
Copyright © 2011-2022 走看看