神龙大侠


私信TA

用户名:H2130823062

访问量:373

签 名:

等  级
排  名 372
经  验 5034
参赛次数 6
文章发表 3
年  龄 0
在职情况 学生
学  校 贺州学院
专  业

  自我简介:

解题思路:广度优先搜索(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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区