对深搜的理解问题——

来源:互联网 发布:亮剑 都梁 知乎 编辑:程序博客网 时间:2024/06/03 17:52

问题:输入一 个数n,输出1~n的全排列。

#include<stdio.h>
#include<iostream>
using namespace std;


int n;//n在1~9之间 
int step; 
int a[10];
int book[10];


void bfs(int step); 
int main()
{
cin>>n;
bfs(1);
getchar();
return 0;



//深搜 
void bfs(int step)
{
if(step==n+1)
{
for(int i=1;i<=n;i++)
{
cout<<a[i];
}
cout<<endl;
return;
}
else
{

for(int j=1;j<=n;j++)
{
if(book[j]==0)
{
a[step]=j;
book[j]=1;
bfs(step+1);
book[j]=0;//用过的牌要收回来不能一直放着 
}
}
}
}

原创粉丝点击