HDU 4665 Unshuffle DFS找一个可行解

每层找一对相等的整数,分别放在两个不同的串中。

参考了学弟的解法,果断觉得自己老了……

#include <cstdio>

#include <cstring>

#include <cstdlib>



const int MAXN = 2010;



int n;

int num[MAXN];

int vis[MAXN];



bool DFS( int st, int last, int cur )

{

    if ( cur == n / 2 ) return true;



    for ( int i = st; i < n; ++i )

        if ( vis[i] == -1 )

        {

            vis[i] = 0;

            for ( int j = i + 1; j < n; ++j )

                if ( num[j] == num[i] && vis[j] == -1 && j > last )

                {

                    vis[j] = 1;

                    if ( DFS( i + 1, j, cur + 1 ) )

                        return true;

                    vis[j] = -1;

                }

            vis[i] = -1;

            break;

        }

    return false;

}



int main()

{

    int T;

    scanf( "%d", &T );

    while ( T-- )

    {

        scanf( "%d", &n );

        for ( int i = 0; i < n; ++i )

            scanf( "%d", &num[i] );



        memset( vis, -1, sizeof(vis) );

        DFS( 0, 0, 0 );

        for ( int i = 0; i < n; ++i )

            printf( "%d", vis[i] );

        puts("");

    }

    return 0;

}

 

你可能感兴趣的:(shuffle)