ACM学习历程—HDU1716 排列2(dfs && set容器)

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

题目要求的就是四个数全排列从小到大输出。

由于考虑到需要排序,而且要去重,所以直接使用了set容器。

全排列用dfs生成。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#define LL long long

using namespace std;

int a[5];
bool vis[5];
set <int> ans;

void dfs(int now, int val)
{
    if (now == 4)
    {
        if (val >= 1000)
            ans.insert(val);
        return;
    }
    for (int i = 0; i < 4; ++i)
    {
        if (vis[i])
            continue;
        vis[i] = true;
        dfs(now+1, val*10+a[i]);
        vis[i] = false;
    }
}

void Work()
{
    ans.clear();
    memset(vis, 0, sizeof(vis));
    dfs(0, 0);
    set <int>::iterator it;
    int high = -1;
    bool flag = false;
    for (it = ans.begin(); it != ans.end(); ++it)
    {
        if (high == -1)
            high = *it/1000;
        if (high != -1 && *it/1000 != high)
        {
            printf("\n");
            high = *it/1000;
            flag = false;
        }
        if (flag)
            printf(" ");
        printf("%d", *it);
        flag = true;
    }
    printf("\n");
}

int main()
{
    //freopen("test.in", "r", stdin);
    bool flag = false;
    while (scanf("%d%d%d%d", &a[0], &a[1], &a[2], &a[3]) != EOF &&
           (a[0]+a[1]+a[2]+a[3]))
    {
        if (flag)
            printf("\n");
        Work();
        flag = true;
    }
    return 0;
}
时间: 2024-10-24 17:42:28

ACM学习历程—HDU1716 排列2(dfs && set容器)的相关文章

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #

ACM学习历程—HDU5269 ZYB loves Xor I(位运算 &amp;&amp; dfs &amp;&amp; 排序)(BestCoder Round #44 1002题)

Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he wants to know the sum of all (lowbit(Ai xor Aj) (i,j∈[1,n]) We define that lowbit(x)=2^k,k is the smallest integer satisfied ((x and 2^k)>0)Specially,

ACM学习历程——HDU5202 Rikka with string(dfs,回文字符串)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: One day, Yuta got a string which contains n letters but Rikka lost it in accident. Now

ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs &amp;&amp; 离线 &amp;&amp; 并查集)

http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时间看这一题.不过打过之前一场BestCoder的应该都会有点思路,虽然BC那题是二维,这题是三维的,但是思路应该是一样的,没错,就是离线加并查集. 正过来考虑的时候,发现第一个要求相邻块是好处理的,但是第二个要求能否到达(1000, 1000, 1000)这个条件似乎比较难判断,当时BC上的题根据题

ACM学习历程—BestCoder Round #75

1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include &l

ACM学习历程—HDU5587 Array(数学 &amp;&amp; 二分 &amp;&amp; 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后从0到末尾,每一个都加上1. 例如:a0, a1, a2 => a0, a1, a2, 1, a0+1, a1+1, a2+1 题解中是这么说的:“ 其实Ai为i二进制中1的个数.每次变化A{k+2^i}=A{k}+1,(k<2^?i??)不产生进位,二进制1的个数加1.然后数位dp统计前m个数二

ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵. 用dfs暴力搜索,不过需要每一步进行判断是否已经出现了重复,如果最后再判断的话复杂度有点高. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring>