求助各路神仙,BFS代码报错。
查看原帖
求助各路神仙,BFS代码报错。
967856
PeaceSunset楼主2023/8/10 12:13

求助大佬——有哪位尊贵的大佬可以指点我一下,求求了!

这份代码我自己倒是看不出来有什么问题,只是报错,希望指正!

#include<bits/stdc++.h>
using namespace std;

queue<string> q;
string end,temp="",moving="12345678";
int dis[1000010];

void A(string e){
	int cnt=dis[e];
	for(int i=0;i<4;i++){
		swap(e[i],e[7-i]);
	}
	if(dis[e]==0){
		q.push(e);
		dis[e]=cnt+1;
		temp=temp+'A';
	}
}

void B(string e){
	int cnt=dis[e];
	string y=e;
	e[0]=y[3],e[1]=y[0],e[2]=y[1],e[3]=y[2];
	e[7]=y[4],e[6]=y[7],e[5]=y[6],e[4]=y[5];
	if(dis[e]==0){
		q.push(e);
		dis[e]=cnt+1;
		temp=temp+'B';
	}
}

void C(string e){
	int cnt=dis[e];
	string y=e;
	e[2]=y[1],e[5]=y[2],e[6]=y[5],e[1]=y[6];
	if(dis[e]==0){
		q.push(e);
		dis[e]=cnt+1;
		temp=temp+'C';
	}
}

void bfs(){
	q.push(moving);
	while(q.size()){
		string x=q.front(),q.pop();
		A(x);
		B(x);
		C(x);
		if(x==end){
			cout << dis[x] << endl << temp;
			return;
		}
	}
}

int main(){
	cin >> end;
	memset(dis,0,sizeof dis);
	bfs();
	return 0;
}

2023/8/10 12:13
加载中...