此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可

 #include <bits/stdc++.h>
using namespace std; int op_a(int n) {//操作A(上下行互换)
int low = n % ;
int high = n / ;
return low * + high;
} int op_b(int n) {//操作B(每次以行循环右移一个)
int low = n % ;
int high = n / ;
int h = high % ;
high = h * + high / ;
int l = low % ;
low = l * + low / ;
return high * + low;
} int op_c(int n) {//操作C(中间四小块顺时针转一格)
int a[];
for (int i = ; i < ; i++) {
a[-i] = n % ;
n = n / ;
}
int ans = a[] * +
a[] * +
a[] * +
a[] * +
a[] * +
a[] * +
a[] * +
a[];
return ans;
}
struct Node {
int num;//num储存状态,用8位的十进制数来储存状态
vector<char> path;//path储存操作
};
Node bfs(int step, int n) {
queue<Node> q;
Node front;
front.num = ;//初始的魔板
q.push(front);
while (!q.empty()) {
front = q.front();
q.pop();
if (front.path.size() > step) {
return front;//如果操作的次数大于step数,返回
}
//依次进行三种操作
Node tmp1 = front;
tmp1.num = op_a(front.num);
tmp1.path.push_back('A');
if (tmp1.num == n) {
return tmp1;//如果得到目标状态则返回
}
else {
q.push(tmp1);
} Node tmp2 = front;
tmp2.num = op_b(front.num);
tmp2.path.push_back('B');
if (tmp2.num == n) {
return tmp2;//如果得到目标状态则返回
}
else {
q.push(tmp2);
} Node tmp3 = front;
tmp3.num = op_c(front.num);
tmp3.path.push_back('C');
if (tmp3.num == n) {
return tmp3;//如果得到目标状态则返回
}
else {
q.push(tmp3);
} }
}
int main(){
int step;
while (cin >> step && step != -) {
int ans = ;
int a;
for (int i = ; i < ; i++) {//将魔板转换成数字
cin >> a;
ans = * ans + a;
}
Node node = bfs(step, ans);
if (node.path.size() > step) cout << "-1" << endl;//如失败则输出-1,否则输出path
else {
cout << node.path.size() << ' ';
for (int i = ; i < node.path.size(); i++) {
cout << node.path[i];
}
cout << endl;
} }
return ;
}

最新文章

  1. GPRS 接入外网的过程
  2. C#模拟键盘输入(一)
  3. Mysql查询阻塞初探
  4. iOS 7.0获取iphone UDID 【转】
  5. python15-day1课堂随机
  6. Spring基础知识及bean的配置
  7. mac linux netstat -n
  8. 简单的实现QQ通信功能(三)
  9. GDI画验证码
  10. Android Volley 之自定义Request
  11. shuffle一个简单的过程叙述性说明
  12. ASP.NET Web安装程序
  13. markdown常用语法教程
  14. 【scikit-learn 0.19 中文文档 】安装 scikit-learn | ApacheCN
  15. C#后台生成验证码
  16. 互相关(cross-correlation)及其在Python中的实现
  17. MSChart的研究(转)
  18. linux下i2c的驱动架构分析和应用
  19. vijos 1128 N个数选K个数 (DFS )
  20. Python+OpenCV图像处理(一)——读取显示一张图片

热门文章

  1. [个人翻译]Redis 集群教程(中)
  2. 两年来的core折腾之路几点总结,附上nginx启用http2拿来即用的配置
  3. Android中关于cpu/cpuset/schedtune的应用
  4. AAU
  5. javascript正则表达式(RegExp)简述
  6. 详解三种缓存过期策略LFU,FIFO,LRU(附带实现代码)
  7. 简单的maven配置
  8. NTFS交换数据流隐写的应用
  9. Codeforces 710F String Set Quries
  10. HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth