深度优先遍历

来源:互联网 发布:什么软件可以看抹油舞 编辑:程序博客网 时间:2024/05/19 02:30
求n个数的所有排列组合。

#include "stdafx.h"#include <stdio.h>int a[10], book[10], n;void dfs(int step){    int i;    if(step == n+1)    {        for (i = 0; i<=n; i++)        {            printf("%d", a[i]);        }        printf("\n");        return;    }    for (i=1; i<=n; i++)    {        if (book[i] == 0)        {            a[step] = i;            book[i] = 1;            dfs(step+1);            book[i] = 0;        }    }    return;}int _tmain(int argc, _TCHAR* argv[]){    scanf("%d", &n);    dfs(1);    system("pause");    return 0;}

 

0 0
原创粉丝点击