传送门ZOJ2477

描述

This is a very popular game for children. In this game, there’s a cube, which consists of 3 3 3 small cubes. We can unwrap the cube, it will become like this:

1
2
3
4
5
6
7
8
9
      w w ww w ww w w
r r r g g g b b b o o o
r r r g g g b b b o o o
r r r g g g b b b o o oy y yy y yy y y

The letters means the color on the small cubes. For example, ‘r’ means red, ‘g’ means green, ‘y’ means yellow….The goal for this game is to rotate the faces of the cube to make each of the faces contains only one color. Note there’re exact 6 kind of colors on the cube and there’re exact 9 small rectangles totally in any time in the game.
Do you know how to rotate the faces? I think most of you have known it. But I would like to show it again. When a face is rotated, the configuration of colors in all the adjacent faces changes. For the cube above, after we rotate the green face clock-wise, the last line of ‘w’ face will become the left column of ‘b’ face, the left column of ‘b’ face will become the top line of ‘y’ face, etc. As you may know, reaching the final position from a scrambled configuration can be quite challenging.
In this problem, you are given a configuration of the cube, and asked to give a way to reach the final position. To reduce the difficulty, the steps required will never be greater than 5.

输入

The input contains an integer in the first line, which indicates the number of the test cases. In each test case, there’re exact 10 lines. The first line is an empty line. The next 9 lines contain a configuration. The format can be seen in the sample input. For simplicity, we give an index to each face as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
    /---\|   || 4 ||   |
/---+---+---+---\
|   |   |   |   |
| 0 | 1 | 2 | 3 |
|   |   |   |   |
\---+---+---+---/|   || 5 ||   |\---/

Note that there’s a space between two adjacent letters.

输出

For each test case, the first line of the output is the smallest count N of the steps to reach the winning position. If the winning position can’t be reached in 5 steps, print -1 in this line. Otherwise print each step in one line in the following N lines. A step contains two integers, the first one means the face index, and the second one means the direction. 1 means clock-wise and -1 means counter clock-wise. If the given position is the winning position, print 0 for such test case simply. If there’re multiple solutions, any one is acceptable.

样例

  • Input

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    2w w ww w ww w w
    r r r g g g b b b o o o
    r r r g g g b b b o o o
    r r r g g g b b b o o oy y yy y yy y yw w ww w wb b b
    r r w g g g y b b o o o
    r r w g g g y b b o o o
    r r w g g g y b b o o or r ry y yy y y
    
  • Output
    0
    1
    1 1

题解

  • 题意:给出魔方六面,每一面标号如上,给出一种排列,你需要把它复原,求最少步数和步骤,不能超过5步。1表示顺时针,-1表示逆时针。
  • 迭代深搜,deep从1到5。贼恶心一题,详细步骤看代码

    Code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    
    #include<bits/stdc++.h>
    #define INIT(a,b) memset(a,b,sizeof(a))
    #define LL long long
    using namespace std;
    const int inf=0x3f3f3f3f;
    const int N=1e6+7;
    const int mod=1e9+7;
    int cube[6][9] = {{4,0,1,2,3,5,6,7,8},{22,9,10,11,21,23,33,34,35},{25,12,13,14,24,26,36,37,38},{28,15,16,17,27,29,39,40,41},{31,18,19,20,30,32,42,43,44},{49,45,46,47,48,50,51,52,53}
    };
    //只有20个需要改变,每个中心无需改变,转动时从i行变成i^1行
    const int rot[12][20]={{11,23,35,34,33,21, 9,10,51,48,45,36,24,12, 6, 3, 0,20,32,44}, { 9,10,11,23,35,34,33,21,36,24,12, 6, 3, 0,20,32,44,51,48,45},{14,13,26,38,37,36,24,12,45,46,47,39,27,15, 8, 7, 6,11,23,35},{12,24,13,14,26,38,37,36,39,27,15, 8, 7, 6,11,23,35,45,46,47},{17,29,41,40,39,27,15,16,47,50,53,42,30,18, 2, 5, 8,14,26,38},{15,16,17,29,41,40,39,27,42,30,18, 2, 5, 8,14,26,38,47,50,53},{18,19,20,32,44,43,42,30,53,52,51,33,21, 9, 0, 1, 2,17,29,41},{42,30,18,19,20,32,44,43,33,21, 9, 0, 1, 2,17,29,41,53,52,51},{ 0, 1, 2, 5, 8, 7, 6, 3,12,13,14,15,16,17,18,19,20, 9,10,11},{ 6, 3, 0, 1, 2, 5, 8, 7,15,16,17,18,19,20, 9,10,11,12,13,14},{45,46,47,50,53,52,51,48,44,43,42,41,40,39,38,37,36,35,34,33},{51,48,45,46,47,50,53,52,41,40,39,38,37,36,35,34,33,44,43,42}
    };
    int deep=0,ans[10][2];//ans[i][0]表示深度为i时转动的面,ans[i][1]表示转动方向
    char a[60];
    bool judge(){for(int i=0;i<6;i++){for(int j=0;j<8;j++){if(a[cube[i][j]]!=a[cube[i][j+1]])return false;}}return true;
    }
    void change(int k){int t=k^1;char tem[60];memcpy(tem,a,sizeof(a));for(int i=0;i<20;i++)a[rot[k][i]]=tem[rot[t][i]];
    }
    bool dfs(int t){if(t>deep) return false;if(judge()) return true;char tem[60];memcpy(tem,a,sizeof(a));for(int i=0;i<12;i++){change(i);ans[t][0]=i/2;ans[t][1]=i&1 ? -1:1;if(dfs(t+1))return true;memcpy(a,tem,sizeof(tem));}return false;
    }
    int main(){int t;scanf("%d",&t);for(int cas=0;cas<t;cas++){if(!cas) getchar();for(int i=0;i<54;i++){while(1){a[i]=getchar();if(a[i]>='a'&&a[i]<='z') break;}}for(deep=0;deep<=6;deep++){if(deep==6) {printf("-1\n");break;}if(dfs(0)) {printf("%d\n",deep);for(int i=0;i<deep;i++)printf("%d %d\n",ans[i][0],ans[i][1]);break;}}}return 0;
    }
    

Magic Cube相关推荐

  1. ZOJ 2477 Magic Cube 三阶魔方还原(IDA*)

    转载请注明出处,谢谢 http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove 三阶魔方还原.因为只搜5层,所以使 ...

  2. cube的意思中文翻译_cube是什么意思_cube的翻译_音标_读音_用法_例句_爱词霸在线词典...

    全部 立方形 立方体 立方 Mr Ursell likens its texture to that of a gravy cube. Ursell先生将它的纹理比作gravycube的纹理. 期刊摘 ...

  3. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  4. 超级计算机性能调查,调查:多数超级计算机使用Linux操作系统

    过去,开发人员为超级计算机定制各种各样的操作系统,超级计算机在操作系统的使用上没有形成统一规定.近年来,这种情况有所改变.Linux系统越来越受欢迎,被很多超级计算机采用.但Linux在超级计算机中究 ...

  5. 长安大学第三届ACM-ICPC程序设计竞赛 L题

    链接: https://www.nowcoder.com/acm/contest/102/L 来源:牛客网 题目描述 Many years later, Rainbow Island is in th ...

  6. 算法学习经典例题整理

    陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...

  7. Linux在超级计算机领域一统天下

    摘要:在世界超级计算机排行榜500强榜单中,基于Linux的超级计算机占据了462个席位,比率高达92%.基于Windows的超级计算机仅有2个席位,份额为0.4%.中国基于Windows的超级计算机 ...

  8. Google Webmaster Tools 结构化数据标记使用入门指南

    要想使自己的产品在Google搜索结果有一个好的排名,我们不仅可以在提高Google搜索排名上下功夫,也可以改善我们的产品在Google搜索结果页的结构化数据样式,这样就能够以更准确.更具吸引力的方式 ...

  9. 美团外卖推荐系统之智能流量分发的实践与探索

    省时查报告-专业.及时.全面的行研报告库 省时查方案-专业.及时.全面的营销策划方案库 [免费下载]2022年12月份热门报告盘点 百度APP Feed流业务架构变迁思考和升级实践 罗振宇2023年跨 ...

  10. ZOJ2477 拼魔方

    每日打卡(1/2) 传送门:点击打开链接 题目大意: 给你一个摊开的魔方,问你是否能在五步之内复原,如果能,输出对应的步骤. 题目思路: 首先需要预处理一下(打表),将各种转动模拟出来. #inclu ...

最新文章

  1. oracle--rowid
  2. vsflexgrid单元格换行后自动使用行高_Excel表格不会换行?10个超实用Excel小技巧,用了都说好!...
  3. linux 磁盘管理3板斧,Linux磁盘管理三板斧的使用心得
  4. java当中有关循环的代码_有关Java循环的内容,编程中还是比较常用的,下面分享给大家几个循环的示例代码,练习一下。1、循环输出1到100之间所有能被3或能被4整除的数。pack...
  5. 来前端君朋友圈,一起聊前端面试技巧
  6. pytorch macos_Windows,Linux和MacOS上的PyTorch安装
  7. linux/ubuntu查看内核版本命令
  8. python2和python3(导包)自定义包并导入之
  9. javaWeb框架开发
  10. java 邮件批量发送邮件_利用Java实现电子邮件的批量发送
  11. 学生信息管理系统报告
  12. 专访王豫翔:编程道路上的“三少三多”(摘录)
  13. linux c 数字字符串互转 相关函数 atoi、atof、atol、atrtod、strtol、strtoul
  14. 小程序开发需要什么步骤?步骤教程分享
  15. VulnHub Billu_b0x
  16. MATLAB上用十一行代码实现深度学习…
  17. 18.3 KSM页面小结
  18. 苹果承认iOS源代码泄露,对iOS 11.2.5的有没有影响
  19. Netstat常用参数及常用CMD命令总结
  20. Navigating to current location (/) is not allowed

热门文章

  1. 计算机辅助英语教学mti,计算机辅助翻译介绍
  2. OpenCV图像处理——阈值处理/二值化(python实现和c++实现)
  3. Extended VINS-Mono: 大规模户外环境进行绝对和相对车辆定位的系统性方法(IROS2021)...
  4. 计算机课题推荐人意见,课题申请推荐人意见怎么写
  5. TCP/IP协议 1 ----实验楼转
  6. 手机里的照片导入计算机的方法,苹果手机照片怎么传到电脑_苹果手机照片传到电脑教程-太平洋IT百科手机版...
  7. google已经启用www.guge.com(谷歌)域名
  8. 二元二次方程例题_二元二次方程组练习题及答案
  9. 用Java计算黄金分割率_java 实现黄金分割数的示例详解
  10. Java基础 - 替罪羊树(Scapegoat Tree)