相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒。关于康托展开可以参考,其可用数学归纳法证明:http://www.cnblogs.com/1-2-3/archive/2011/04/25/generate-permutation-part2.html

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. map<int, int>visit;
  4. int op_a(int n) {//操作A(上下行互换)
  5. int low = n % ;
  6. int high = n / ;
  7. return low * + high;
  8. }
  9.  
  10. int op_b(int n) {//操作B(每次以行循环右移一个)
  11. int low = n % ;
  12. int high = n / ;
  13. int h = high % ;
  14. high = h * + high / ;
  15. int l = low % ;
  16. low = l * + low / ;
  17. return high * + low;
  18. }
  19.  
  20. int op_c(int n) {//操作C(中间四小块顺时针转一格)
  21. int a[];
  22. for (int i = ; i < ; i++) {
  23. a[-i] = n % ;
  24. n = n / ;
  25. }
  26. int ans = a[] * +
  27. a[] * +
  28. a[] * +
  29. a[] * +
  30. a[] * +
  31. a[] * +
  32. a[] * +
  33. a[];
  34. return ans;
  35. }
  36. struct Node {
  37. int num;//num储存状态,用8位的十进制数来储存状态
  38. vector<char> path;//path储存操作
  39. };
  40. Node bfs(int step, int n) {
  41. queue<Node> q;
  42. Node front;
  43. front.num = ;//初始的魔板
  44. visit[front.num] = ;
  45. q.push(front);
  46. if(front.num == n)return front;
  47.  
  48. while (!q.empty()) {
  49. front = q.front();
  50. q.pop();
  51. if (front.path.size() > step) {
  52. return front;//如果操作的次数大于step数,返回
  53. }
  54. //依次进行三种操作
  55. Node tmp1 = front;
  56.  
  57. tmp1.num = op_a(front.num);
  58. tmp1.path.push_back('A');
  59. if (tmp1.num == n) {
  60. return tmp1;
  61. }
  62. else if(visit.find(tmp1.num) == visit.end()){
  63. visit[tmp1.num] = ;
  64. q.push(tmp1);
  65. }
  66.  
  67. Node tmp2 = front;
  68. tmp2.num = op_b(front.num);
  69. tmp2.path.push_back('B');
  70. if (tmp2.num == n) {
  71. return tmp2;
  72. }
  73. else if(visit.find(tmp2.num) == visit.end()){
  74. visit[tmp2.num] = ;
  75. q.push(tmp2);
  76. }
  77.  
  78. Node tmp3 = front;
  79. tmp3.num = op_c(front.num);
  80. tmp3.path.push_back('C');
  81. if (tmp3.num == n) {
  82. return tmp3;
  83. }
  84. else if(visit.find(tmp3.num) == visit.end()){
  85. visit[tmp3.num] = ;
  86. q.push(tmp3);
  87. }
  88.  
  89. }
  90. }
  91. int main(){
  92. int step;
  93. while (cin >> step && step != -) {
  94. int ans = ;
  95. int a;
  96. for (int i = ; i < ; i++) {//将魔板转换成数字
  97. cin >> a;
  98. ans = * ans + a;
  99. }
  100.  
  101. visit.clear();
  102. Node node = bfs(step, ans);
  103. if (node.path.size() > step) cout << "-1" << endl;//如失败则输出-1,否则输出path
  104. else {
  105. cout << node.path.size() << " ";
  106. for (int i = ; i < node.path.size(); i++) {
  107. cout << node.path[i];
  108. }
  109. cout << endl;
  110. }
  111.  
  112. }
  113. return ;
  114. }

Sicily 1051: 魔板(BFS+排重)的更多相关文章

  1. hdu1430魔板(BFS+康托展开)

    做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...

  2. hdu.1430.魔板(bfs + 康托展开)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. Sicily 1151 魔板

    Constraints Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge Description 魔板由8个大小相同方块组成,分别用涂上不 ...

  4. HDU - 1430 魔板 (bfs预处理 + 康托)

    对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...

  5. hdu 1430 魔板 (BFS+预处理)

    Problem - 1430 跟八数码相似的一题搜索题.做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可. 代码如下: ...

  6. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

  7. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  8. 哈希+Bfs【P2730】 魔板 Magic Squares

    没看过题的童鞋请去看一下题-->P2730 魔板 Magic Squares 不了解康托展开的请来这里-->我这里 至于这题为什么可以用康托展开?(瞎说时间到. 因为只有8个数字,且只有1 ...

  9. 【搜索】魔板问题(BFS)

    [搜索]魔板问题 时间限制: 1 Sec  内存限制: 64 MB提交: 5  解决: 3[提交][状态][讨论版] 题目描述 据说能使持有者成为世界之主的上古神器隐藏在魔板空间,魔板由8个同样大小的 ...

随机推荐

  1. [LeetCode] Russian Doll Envelopes 俄罗斯娃娃信封

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  2. [LeetCode] Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  3. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  4. solr.net的使用

    引子 最近在做一个日志系统,用普通关系型数据库做数据查询遇到了查询的瓶颈,想到了用成熟的搜索应用服务,我所知道的比较成熟的搜索应用服务有solr和es(elasticsearch),由于时间比较仓促, ...

  5. Ceph RGW服务 使用s3 java sdk 分片文件上传API 报‘SignatureDoesNotMatch’ 异常的定位及规避方案

    import java.io.File;   import com.amazonaws.AmazonClientException; import com.amazonaws.auth.profile ...

  6. 常用的shell脚本

    [root@WEB1-live sh]# cat licai_fabu.sh #!/bin/bash pid=` ps -ef | grep java | grep '8011' | awk '{pr ...

  7. 关于CAJViewer 无法获取document路径问题

    修改注册表 进入注册表编辑器: win+R >>输入 regedit  如下图: 修改关键注册表项(两项相同值应同时修改) 1.HKEY_CURRENT_USER\Software\Mic ...

  8. bzoj1251

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3776  Solved: 1581[Submit][Status][Discu ...

  9. perl

    introduction: http://www.yiibai.com/perl/perl_introduction.html functions: http://www.yiibai.com/per ...

  10. AndroidStudio 1.4配置NDK

    AndroidStudio(AS) 1.3之后已经支持NDK,这为NDK开发提供了极大的便利,不在需要配置各种头疼的MK文件,简单的九步就可完成配置.要说明的是,第一次配置AS一定要有耐心. 0,下载 ...