Uva_331
I was planning to useBFSFind the minimum number of exchanges, but find that there are too many types of data, and the array cannot be placed. Later, we found that the process of sorting with the bubble method corresponds to the minimum number of exchanges.
After obtaining the minimum number of exchanges, you only need to limit the number of layers of deep search, and then recursively find the total number of exchange solutions.
# Include < Stdio. h >
# Include < String . H >
# Include < Stdlib. h >
Int A [ 5 ], Target [ 5 ], Num, N, ans;
Void DFS ( Int Cur, Int P [])
{
Int I, K, Q [ 5 ];
If (Cur = Num)
{
If (Memcmp (p, target, Sizeof (Target )) = 0 )
Ans ++ ;
Return ;
}
For (I = 0 ; I < N - 1 ; I ++ )
{
Memcpy (Q, P, Sizeof (Q ));
K = Q [I];
Q [I] = Q [I + 1 ];
Q [I + 1 ] = K;
DFS (cur + 1 , Q );
}
}
Int Main ()
{
Int I, J, K, T, front, rear;
T = 0 ;
While ( 1 )
{
Scanf ( " % D " , & N );
If (N = 0 )
Break ;
Memset (, 0 , Sizeof ());
For (I = 0 ; I < N; I ++ )
Scanf ( " % D " , & A [I]);
Memcpy (target,, Sizeof ());
Num = 0 ;
For (I = 0 ; I < N; I ++ )
For (J = N - 1 ; J > I; j -- )
If (Target [J] < Target [J - 1 ])
{
K = Target [J];
Target [J] = Target [J - 1 ];
Target [J - 1 ] = K;
Num ++ ;
}
If (Num = 0 )
{
Printf ( " There are 0 swap maps for input data set % d. \ n " , ++ T );
Continue ;
}
Ans = 0 ;
DFS ( 0 , );
Printf ( " There are % d swap maps for input data set % d. \ n " , ANS, ++ T );
}
Return 0 ;
}