解题思路:广度优先搜索(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语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:780 |
C语言训练-排序问题<1> (C语言代码)浏览:1348 |
C语言训练-角谷猜想 (C++代码)(3N+1问题)浏览:1745 |
矩阵乘法 (C++代码)浏览:1454 |
妹子杀手的故事 (C语言代码)浏览:1218 |
C语言程序设计教程(第三版)课后习题8.2 (C语言代码)浏览:5228 |
循环入门练习5 (C语言代码)浏览:829 |
printf基础练习2 (C语言代码)浏览:503 |
大神老白 (C语言代码)浏览:600 |
淘淘的名单 (C语言代码)浏览:1222 |