unordered_map 来存状态
对于访问过数组的情况怎么设置 访问标记 有很多种方法,string类型算是比较方便的了
#include <bits/stdc++.h>
using namespace std;
int a[8];
unordered_map<string,char> d;
unordered_map<string,int> vis;
unordered_map<string,string> pre;
int main()
{
string s = "12348765";
string t;
for(int i = 0; i < 8; ++i)
{
char c; cin >> c;
t += c;
}
swap(t[4],t[7]);swap(t[5],t[6]);
queue<string> q; q.push(s); vis[s] = 1,pre[s]= "-1";
while(q.size())
{
string fa = q.front(); q.pop();
string s1 = fa.substr(4,4) + fa.substr(0,4);
string s2 = fa[3] + fa.substr(0,3) + fa[7] +fa.substr(4,3);
string s3 = fa; swap(s3[1],s3[2]); swap(s3[1],s3[6]); swap(s3[1],s3[5]);
if(vis[s1] == 0) q.push(s1), vis[s1] = 1,d[s1] = 'A',pre[s1]=fa;
if(vis[s2] == 0) q.push(s2), vis[s2] = 1,d[s2] = 'B',pre[s2]=fa;
if(vis[s3] == 0) q.push(s3), vis[s3] = 1,d[s3] = 'C',pre[s3]=fa;
}
vector<string> v;
while(t != s)
{
v.push_back(t);
t = pre[t];
}
reverse(v.begin(),v.end());
cout << v.size() << "\n";
for(int i = 0; i < v.size(); ++i) cout << d[v[i]];
}