HDU 1716:排列2(全排列)

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/wang_123_zy/article/details/83153474

排列2

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11462    Accepted Submission(s): 4045

Problem Description

Ray又对数字的列产生了兴趣:
现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。

Input

每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。

Output

对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。
每组输出数据间空一行,最后一组数据后面没有空行。

Sample Input

 

1 2 3 4
1 1 2 3
0 1 2 3
0 0 0 0

Sample Output

1234 1243 1324 1342 1423 1432
2134 2143 2314 2341 2413 2431
3124 3142 3214 3241 3412 3421
4123 4132 4213 4231 4312 4321

1123 1132 1213 1231 1312 1321
2113 2131 2311
3112 3121 3211

1023 1032 1203 1230 1302 1320
2013 2031 2103 2130 2301 2310
3012 3021 3102 3120 3201 3210

思路

直接调用next_permutation 或用DFS,注意输出的问题

next_permutation传送门

AC代码

next_permutation

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <time.h>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
#define bug cout<<"-------------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int ar[5];
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	#ifndef ONLINE_JUDGE
	    freopen("in.txt", "r", stdin);
	    freopen("out.txt", "w", stdout);
	    double _begin_time = clock();
	#endif
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	while(1)
	{
		if(a==0&&b==0&&c==0&&d==0)
			break;
		ar[0]=a;
		ar[1]=b;
		ar[2]=c;
		ar[3]=d;
		sort(ar,ar+4);
		int res=ar[0];
		if(ar[0])
			cout<<ar[0]<<ar[1]<<ar[2]<<ar[3];
		while(next_permutation(ar,ar+4))
	    {
	    	if(ar[0]==res&&ar[0])
	    		cout<<" "<<ar[0]<<ar[1]<<ar[2]<<ar[3];
	    	else
	    	{
	    		if(ar[0])
	    		{
	    			if(res)
	    				cout<<endl;
	    			for(int i=0;i<4;i++)
	    				cout<<ar[i];
	    		}
	    	}
	    	res=ar[0];
	    }
	    cout<<endl;
	    cin>>a>>b>>c>>d;
	    if(a||b||c||d)
	    	cout<<endl;
	}
	#ifndef ONLINE_JUDGE
	    long _end_time = clock();
	    printf("time = %lf ms.", _end_time - _begin_time);
	#endif
	return 0;
}

DFS

#include<bits/stdc++.h>
#define MAX 5
int flag[MAX];
int n,j,k,p,zz;
int tem[MAX];
int max;
int a[5];
void DFS(int j)
{
    int i;
    // dfs的停止条件:j>4(当前tem数组里有4个数字)
    // 停止的时候,输出当前全排列的数字
    if(j>4)
    {   
        int s=0;
        for(i=1;i<=4;i++)
            s=s*10+tem[i];
        if(s>max)
        {
            if(p==0)
            {
                p++;    
                printf("%04d",s);
            }
            else
            {
                // 判断是否换行
                if(s/1000==zz/1000)
                    printf(" %04d",s);
                else 
                    printf("\n%04d",s);
            }
            max=s;
        }
        zz=s;
    }       
    // flag[i]表示数字a[i]是否被访问(是否被放到tem数组中)
    for(i=1;i<=4;i++)
    {
        // 如果该数字没有被访问
        if(flag[i]!=0)
        {
            // 将未被标记的a数组里的数字放到tem数组中
            tem[j]=a[i];
            // 当前加入到tem中的数字进行标记
            flag[i]=0; 
            // 进行递归
            DFS(j+1);
            // 回溯,将flag[i]变为未被标记的状态
            flag[i]=1;
        }
    }
}
int main()
{
    int pp=1;
    while(pp++)
    {
        p=0;max=999;
        for( j=1;j<=4;j++)
        {
            scanf("%d",&a[j]);
            flag[j]=j;
        }
        if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0) 
            break;
        std::sort(a+1,a+5);
        if(pp>2)
            printf("\n");
        DFS(1);    
        printf("\n");
    }
}

猜你喜欢

转载自blog.csdn.net/wang_123_zy/article/details/83153474
今日推荐