解题思路:广度优先搜索(bfs)
注意事项:
参考代码:
#include<bits/stdc++.h> using namespace std; string s,t,u; queue<string>q; map<string,string>m; string fa(string x) { swap(x[0], x[7]); swap(x[1], x[6]); swap(x[2], x[5]); swap(x[3], x[4]); return x; } string fb(string x) { swap(x[0], x[3]); swap(x[4], x[5]); swap(x[1], x[3]); swap(x[5], x[6]); swap(x[2], x[3]); swap(x[6], x[7]); return x; } string fc(string x) { swap(x[1], x[2]); swap(x[5], x[6]); swap(x[1], x[5]); return x; } void bfs() { q.push("12345678"); m["12345678"] = ""; while(!q.empty()) { u = q.front(); q.pop(); if(m.count(s) == 1) { cout<<m[s].size()<<endl<<m[s]<<endl; //cout<<s<<endl; return ; } t = fa(u); if(m.count(t) == 0) { q.push(t); m[t] =m[u] + "A"; } t = fb(u); if(m.count(t) == 0) { q.push(t); m[t] =m[u] + "B"; } t = fc(u); if(m.count(t) == 0) { q.push(t); m[t] = m[u] + "C"; } } } int main() { for(int i = 0; i < 8; i++) { cin >> t; s += t; } //cout<<s << endl; bfs(); return 0; }
0.0分
1 人评分
C二级辅导-计负均正 (C语言代码)浏览:581 |
C语言训练-求矩阵的两对角线上的元素之和 (C语言代码)浏览:3396 |
C二级辅导-计负均正 (C语言代码)浏览:658 |
弟弟的作业 (C++代码)浏览:1304 |
剔除相关数 (C语言代码)浏览:1015 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:481 |
C语言训练-求函数值 (C语言代码)浏览:938 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:517 |
计算质因子 (C++代码)浏览:1644 |
字符串比较 (C语言代码)答案错误????浏览:597 |