You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
//此处特别说明下: C语言的全局变量在没有赋值前默认为0, 因此这里的book数组无需全部再次赋初值0
int a [ 10 ] , book [ 10 ] , n ;
//step代表现在在第几个盒子前面
void dfs ( int step ) {
int i ;
if ( step = = n + 1 ) {
//如果站在第n+1个盒子前面, 则表示前n个盒子已经放好扑克牌
//输出一种排列( 1~n号盒子中的扑克牌编号)
for ( i = 1 ; i < = n ; i + + ) {
printf ( " %d " , a [ i ] ) ;
}
printf ( " \n " ) ;
return ; //返回之前的一步( 最近一次调用dfs函数的地方)
}
//此时站在第step个盒子面前, 应该放那张牌呢
//按照1, 2, 3。。。n的顺序一一尝试
for ( i = 1 ; i < = n ; i + + ) {
//判断扑克牌i是否在手上
if ( book [ i ] = = 0 ) {
//开始尝试使用扑克牌
a [ step ] = i ; //将i号扑克牌放入第step号盒子中
book [ i ] = 1 ; //将book[i]等于0表示i号扑克牌在手上
//第step个盒子已经放好了扑克牌, 接下来需要走到下一个盒子前面
dfs ( step + 1 ) ; //这是通过函数的递归调用实现的(最近调用自己)
book [ i ] = 0 ; //这是非常重要的一步,一定要将刚才尝试的扑克牌收回,才能进行下一次尝试
}
}
return ;
}
int main ( int argc , const char * argv [ ] ) {
scanf ( " %d " , & n ) ; //输入时要注意n为1~9之间的整数
dfs ( 1 ) ; //首先站在1号小盒子前面
return 0 ;
}