/*赤裸裸的 模板题 生成可重集的全排列,LRJ 大神白皮书上的代码copy过来的*/
#include <stdio.h>
#include <string.h>
int A[200], P[200];
void print_permutation( int n, int *P, int *A, int cur )
{
int i, j, c1, c2;
if( cur == n )
{
for( i = 0; i < n; i++ )
printf( "%d", A[i] );
printf( "\n" );
}
else
for( i = 0; i < n; i++ )
{
if( !i || P[i] != P[i-1] )
{
c1 = c2 = 0;
for( j = 0; j < cur; j++ ) if( A[j] == P[i] ) c1++;
for( j = 0; j < n; j++ ) if( P[i] == P[j] ) c2++;
if( c1 < c2 )
{
A[cur] = P[i];
print_permutation( n, P, A, cur+1 );
}
}
}
}
int main()
{
int n, time, h, i, j;
scanf( "%d\n", &time );
while( time-- )
{
memset( P, 0, sizeof(P));
scanf( "%d%d", &n, &h );
j = n-1;
for( i = 0; i < h; i++ )
P[j--] = 1;
print_permutation( n, P, A, 0 );
if( time != 0 )
printf( "\n" );
}
return 0;
}
UVA 729 全排列dfs
最新推荐文章于 2019-11-16 20:55:28 发布