大佬求调试
查看原帖
大佬求调试
1029575
Alg_orithm楼主2024/7/16 10:43
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
map <string,int>m;
queue <string>q;
int dx[4]={-3,-1,1,3};
int main()
{
	string end,start="123804765",s;
	cin>>end;
	int k;
	q.push(start);
	m[start]=1;
	while(!q.empty())
	{
		s=q.front();
		k=m[s];
	
		int x;
		while(s[x]!=48)x++;
		for(int i=0;i<4;i++)
		{
			if(i==1&&(x==3||x==6))continue;
			if(i==2&&(x==2||x==5))continue;
			string t=s;
			int y=x+dx[i];
			if(y<0||y>8)continue;
			swap(t[x],t[y]);
			cout<<t<<endl;
			if(!m.count(t))
			{
				q.push(t);
				m[t]=k+1;
			}
		}
		if(m.count(end))
		{
			cout<<m[end]-1;
			return 0;
		}
		q.pop();
	}
}
2024/7/16 10:43
加载中...