题目太长了直接放链接:
https://nanti.jisuanke.com/t/17317
题意:给你两个n个数的全排列,例如n=4时,1234、1324、4312这些都是
你每次可以交换其中两个数字,必须交换掉第一个,问至少交换多少次可以将第一个全排列变成第二个
9!=362880,直接爆搜
用map[]当做标记数组
#include<stdio.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
map<int, int> p;
queue<int> q;
char str[15];
int main(void)
{
int n, i, x, y, z, temp;
scanf("%d", &n);
while(scanf("%d%d", &x, &y)!=EOF)
{
p[x] = 0;
q.push(x);
while(q.empty()==0)
{
x = q.front();
q.pop();
if(x==y)
{
printf("%d\n", p[y]);
while(q.empty()==0)
q.pop();
p.clear();
break;
}
temp = p[x];
for(i=2;i<=n;i++)
{
sprintf(str+1, "%d", x);
swap(str[1], str[i]);
sscanf(str+1, "%d", &z);
if(p.count(z)==0)
{
p[z] = temp+1;
q.push(z);
}
}
}
}
return 0;
}