hdu 2611 DFS

来源:互联网 发布:页面静态化 java 编辑:程序博客网 时间:2024/05/18 03:34

日!

AC代码如下:

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <set>using namespace std;#define MAX 0x3f3f3f3fstruct Node{int num, index;};int N, P, cnt, record[110];Node nodes[110];int cmp( const void *a, const void *b ){if( ((Node*)a)->num == ((Node*)b)->num ){return ((Node*)a)->index - ((Node*)b)->index;} return ((Node*)a)->num - ((Node*)b)->num;}void show( int pos ){printf( "%d", record[0] );for( int i = 1; i <= pos; i++ ){printf( " %d", record[i] );}cout << endl;}bool DFS( int pos, int deep, int maxdeep, int preindex ){if( cnt >= P ){return true;}bool f = false;int pre;for( int i = pos; i < N; i++ ){if( nodes[i].index > preindex ){if( !f ){f = true;pre = nodes[i].num;}else if( pre == nodes[i].num ){continue;}pre = nodes[i].num;record[deep] = nodes[i].num;if( deep == maxdeep ){show( deep );cnt++;if( cnt == P ){//这里不能少。。。。。。。。return true;}}else if( DFS( i + 1, deep + 1, maxdeep, nodes[i].index ) ){return true;}}}return false;}int main(){while( scanf( "%d%d", &N, &P ) != EOF ){for( int i = 0; i < N; i++ ){scanf( "%d", &nodes[i].num );nodes[i].index = i;}qsort( nodes, N, sizeof( Node ), cmp );cnt = 0;for( int i = 0; i < N; i++ ){if( DFS( 0, 0, i, -1 ) ){break;}if( cnt >= P ){break;}}cout << endl;}return 0;}


0 0