跳到正文

个人记录

122121

zqrmon约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年7月11日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 3 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
    string ed;
    char c[8];
    bool b=0;
    for (int i = 0; i < 8; ++i) {
        cin >> c[i];
        if(c[i]!=i+1+'0'){
        	b=1;
		}
        ed += c[i];
    }
    if(b==0){
    	cout<<0;
	}
    queue<string> q;
    map<string, string> mp;
    q.push("12345678");
    mp["12345678"] = "";
    while (!q.empty()) {
        string s = q.front(); q.pop();
        {
            string x = s;
            for (int i = 0; i < 4; ++i)
                swap(x[i], x[7 - i]);
            if (mp.count(x) == 0) {
                mp[x] = mp[s] + 'A';
                q.push(x);
                if (x == ed) {
                    cout << mp[x].size() << endl << mp[x];
                    return 0;
                }
            }
        }
        {
            string x = s;
            x[0] = s[3]; x[1] = s[0]; x[2] = s[1]; x[3] = s[2];
            x[4] = s[5]; x[5] = s[6]; x[6] = s[7]; x[7] = s[4];
            if (mp.count(x) == 0) {
                mp[x] = mp[s] + 'B';
                q.push(x);
                if (x == ed) {
                    cout << mp[x].size() << endl << mp[x];
                    return 0;
                }
            }
        }
        {
            string x = s;
            x[1] = s[6]; x[2] = s[1]; x[5] = s[2]; x[6] = s[5];
            if (mp.count(x) == 0) {
                mp[x] = mp[s] + 'C';
                q.push(x);
                if (x == ed) {
                    cout << mp[x].size() << endl << mp[x];
                    return 0;
                }
            }
        }
    }

    return 0;
}
原文