好水的一道题
第一次想多了
只要每次只交换大的和下的不做多余的交换移动次数一定是最小的
#include<iostream>
using namespace std;
int n,sum,a[5];
bool isorder()
{
for (int i=0; i<n-1; i++)
if (a[i]>a[i+1])
return false;
return true;
}
void dfs()
{
int tmp,i;
if (isorder())
sum++;
else
{
for(i=0; i<n-1; i++)
{
if (a[i]>a[i+1])
{
tmp=a[i];
a[i]=a[i+1];
a[i+1]=tmp;
dfs();
tmp=a[i];
a[i]=a[i+1];
a[i+1]=tmp;
}
}
}
}
int main()
{
int col=0;
while (cin>>n&&n)
{
col++;
for (int i=0; i<n; i++)
cin>>a[i];
sum=0;
if (!isorder())
dfs();
cout<<"There are "<<sum<<" swap maps for input a set "<<col<<"."<<endl;
}
return 0;
}