#include<bits/stdc++.h>
using namespace std;
string a,b;
int n;
map<string,int>mapp;
void bfs(){
mapp.clear();
queue<string> Q;
queue<int>Qint;
string tm="";
string mm;
while(Q.size())
{
Q.pop();
}
while(Qint.size())
{
Qint.pop();
}
Q.push(a);
Qint.push(0);
int step=0;
int mmint=0;
while(Q.size())
{
mm=Q.front();
mmint=Qint.front();
Qint.pop();
Q.pop();
for(int i=1;i<n;i++)
{
tm=mm[i]+mm.substr(1,i-1)+mm[0]+mm.substr(i+1,n-1);
if(tm==b)
{
cout<<mmint+1<<endl;
return;
}
else if(mapp.count(tm)==0)
{
mapp[tm]=1;
Q.push(tm);
Qint.push(mmint+1);
}
}
}
return;
}
int main()
{
scanf("%d",&n);
int nm=5;
while(nm--)
{
cin>>a>>b;
bfs();
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
map<char, int> pos;
char st[11], en[11];
int n;
bool OK()
{
for(int i=0; i<n; i++){
if(st[i] != en[i])
return false;
}return true;
}
int main(void)
{
freopen("e://duipai//data.txt","r",stdin);
freopen("e://duipai//out1.txt","w",stdout);
scanf("%d", &n);
for(int t=1; t<=2520; t++){
pos.clear();
scanf("%s%s", st, en);
for(int i=0; i<n; i++)
{
pos[en[i]] = i;///记录每一个数字的正确位置
}
int ans = 0;
int p = 0;
while(!OK()){///每一次检查一下序列是否已经变成了最终序列
if(en[0] != st[0]){
swap(st[0], st[pos[st[0]]]);
ans++;
}else{
for(int i=1; i<n; i++){
if(en[i] != st[i]){
swap(st[0], st[i]);
ans++;
break;
}
}
}
}
printf("%d\n", ans);
}
return 0;
}
网友评论