本程序在VS2013下调试通过,若有疑问可以评论大家探讨
// DFS.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int a[10], book[10], n;
void dfs(int step){
int i;
if (step == n + 1){
for (i = 1; i <= n; i++){
printf_s("%d",a[i]);
}
printf_s("\n");
}
for (i = 1; i <= n; i++){
if (book[i] == 0)
{
a[step] = i;
book[i] = 1;
dfs(step + 1);
book[i] = 0;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
scanf_s("%d",&n);
dfs(1);
system("pause");
return 0;
}