样例挂了,我不理解
查看原帖
样例挂了,我不理解
392157
Little_CartG2_leaf楼主2022/9/6 22:06

rt

虽说这道题是状压,但是我不理解这个代码为什么连样例都过不去。

悬赏?可能有 [0,4][0,4] 个关注吧,别的没了。

#include<bits/stdc++.h>
using namespace std;
string bg;
char a;
map<string,string> m;
queue<string> q;
int ca[9]={8,7,6,5,1,2,3,4};
int cb[9]={4,1,2,3,5,8,7,6};
int cc[9]={1,7,2,4,8,6,3,5};
void doa(string st){
	string ed;
	ed="";
	for(int i=0;i<=7;i++){
		ed+=st[ca[i]-1];
	}
	if(m.count(ed)==0){
		q.push(ed);
		m[ed]=m[st]+'A';
	}
	return;
}
void dob(string st){
	string ed;
	ed="";
	for(int i=0;i<=7;i++){
		ed+=st[cb[i]-1];
	}
	if(m.count(ed)==0){
		q.push(ed);
		m[ed]=m[st]+'B';
	}
	return;
}
void doc(string st){
	string ed;
	ed="";
	for(int i=0;i<=7;i++){
		ed+=st[cc[i]-1];
	}
	if(m.count(ed)==0){
		q.push(ed);
		m[ed]=m[st]+'C';
	}
	return;
}
void bfs(){
	q.push("12345678");
	m["12345678"]="";
	while(!q.empty()){
		doa(q.front());
		dob(q.front());
		doc(q.front());
		if(m.count(bg)!=0){
			cout<<m[bg].size()<<"\n"<<m[bg];
			return;
		}
		q.pop();
	}
}
int main(){
	for(int i=0;i<=7;i++){
		cin>>a;
		bg+=a;
	}
	bfs();
	return 0;
}

2022/9/6 22:06
加载中...