Karl___ 2021-11-14 17:30 采纳率: 100%
浏览 38
已结题

用DFS来求一个集合的子集(!!TAT)

数组中存放10个数,为{0、1、2、3、4、5、6、7、8、9}
请输出它的子集
请用DFS算法来解答,请使用C
【小白被这道题困扰好几天了】

  • 写回答

3条回答 默认 最新

  • CSDN专家-Time 2021-11-15 14:11
    关注

    https://blog.csdn.net/kid_a1412/article/details/114156220

    #include <stdio.h>
    // 栈
    typedef struct Node {
        // 是否已访问
        int visited;
        // 值
        int value;
    } Node;
    Node NullValue;
    typedef struct stack{
        int length;
        Node arrs[10];
    }s1;
    s1 stack;
    
    Node node[10];
    int arrs[10] = { 0,1,2,3,4,5,6,7,8,9 };
    int getLength() {
        return stack.length;
    }
    int getHead() {
        return stack.arrs[stack.length].value;
    }
    int push(Node a) {
        stack.arrs[stack.length++] = a;
    }
    void pop() {
        stack.arrs[stack.length--] = NullValue;
    }
    void display() {
        for (int i = 0; i < stack.length; i++) {
            printf("%d ", stack.arrs[i].value);
        }
        printf("\n");
    }
    
    
    int main() {
        int length;
        int j = 0;
        int index,now;
        int i;
        NullValue.value = -1;
        stack.length = 0;
        // 把数组当作 树
        //        0
        //     1    2
        //    3 4  5 6
        //  7 8 9
        
        for (i = 0; i < 10; i++) {
            node[i].value = arrs[i];
            node[i].visited = 0;
        }
        
        for (i = 0; i < 10; i++) {
            // 结果
            length = getLength();
            for (j = 0; j <length; j++) {
                pop();
            }
            index = i;
            now = i;
            push(node[i]);
            node[i].visited = 1;
            display();
            index++;
            while (getLength() != 10 - i) {
                if (index < 10) {
                    if (node[index].visited == 0) {
                        push(node[index]);
                        node[index].visited = 1;
                        display();
                        pop();
                        index++;
                    }
                }
                else {
                    index = ++now;
                    push(node[index]);
                    index++;
                    for (j = 0; j < 10; j++) {
                        node[j].visited = 0;
                    }
                }
            }
            
        }
        
        
        return 0;
    }
    
    0
    0 1
    0 2
    0 3
    0 4
    0 5
    0 6
    0 7
    0 8
    0 9
    0 1 2
    0 1 3
    0 1 4
    0 1 5
    0 1 6
    0 1 7
    0 1 8
    0 1 9
    0 1 2 3
    0 1 2 4
    0 1 2 5
    0 1 2 6
    0 1 2 7
    0 1 2 8
    0 1 2 9
    0 1 2 3 4
    0 1 2 3 5
    0 1 2 3 6
    0 1 2 3 7
    0 1 2 3 8
    0 1 2 3 9
    0 1 2 3 4 5
    0 1 2 3 4 6
    0 1 2 3 4 7
    0 1 2 3 4 8
    0 1 2 3 4 9
    0 1 2 3 4 5 6
    0 1 2 3 4 5 7
    0 1 2 3 4 5 8
    0 1 2 3 4 5 9
    0 1 2 3 4 5 6 7
    0 1 2 3 4 5 6 8
    0 1 2 3 4 5 6 9
    0 1 2 3 4 5 6 7 8
    0 1 2 3 4 5 6 7 9
    0 1 2 3 4 5 6 7 8 9
    1
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    1 9
    1 2 3
    1 2 4
    1 2 5
    1 2 6
    1 2 7
    1 2 8
    1 2 9
    1 2 3 4
    1 2 3 5
    1 2 3 6
    1 2 3 7
    1 2 3 8
    1 2 3 9
    1 2 3 4 5
    1 2 3 4 6
    1 2 3 4 7
    1 2 3 4 8
    1 2 3 4 9
    1 2 3 4 5 6
    1 2 3 4 5 7
    1 2 3 4 5 8
    1 2 3 4 5 9
    1 2 3 4 5 6 7
    1 2 3 4 5 6 8
    1 2 3 4 5 6 9
    1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7 9
    1 2 3 4 5 6 7 8 9
    2
    2 3
    2 4
    2 5
    2 6
    2 7
    2 8
    2 9
    2 3 4
    2 3 5
    2 3 6
    2 3 7
    2 3 8
    2 3 9
    2 3 4 5
    2 3 4 6
    2 3 4 7
    2 3 4 8
    2 3 4 9
    2 3 4 5 6
    2 3 4 5 7
    2 3 4 5 8
    2 3 4 5 9
    2 3 4 5 6 7
    2 3 4 5 6 8
    2 3 4 5 6 9
    2 3 4 5 6 7 8
    2 3 4 5 6 7 9
    2 3 4 5 6 7 8 9
    3
    3 4
    3 5
    3 6
    3 7
    3 8
    3 9
    3 4 5
    3 4 6
    3 4 7
    3 4 8
    3 4 9
    3 4 5 6
    3 4 5 7
    3 4 5 8
    3 4 5 9
    3 4 5 6 7
    3 4 5 6 8
    3 4 5 6 9
    3 4 5 6 7 8
    3 4 5 6 7 9
    3 4 5 6 7 8 9
    4
    4 5
    4 6
    4 7
    4 8
    4 9
    4 5 6
    4 5 7
    4 5 8
    4 5 9
    4 5 6 7
    4 5 6 8
    4 5 6 9
    4 5 6 7 8
    4 5 6 7 9
    4 5 6 7 8 9
    5
    5 6
    5 7
    5 8
    5 9
    5 6 7
    5 6 8
    5 6 9
    5 6 7 8
    5 6 7 9
    5 6 7 8 9
    6
    6 7
    6 8
    6 9
    6 7 8
    6 7 9
    6 7 8 9
    7
    7 8
    7 9
    7 8 9
    8
    8 9
    9
    
    

    课后作业:用递归函数描述这个方法如何实现的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月31日
  • 已采纳回答 8月23日
  • 创建了问题 11月14日

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂