牛客(dfs&bfs)--- 全排列

老子的全排列呢

#include<bits/stdc++.h>

using namespace std;

int orders[10];//8个位置 
int chosen[10];//标记 

void dfs(int x){
	if(x>=9){//如果超过8 
		for(int i=1;i<=8;i++){
			printf("%d ",orders[i]);
		}
		puts("");
		return ;
	}
	for(int i=1;i<=8;i++){//枚举所有的情况 
		if(!chosen[i]){
			chosen[i]=1;//标记已经用过了 
			orders[x]=i;//将i放入x的位置 
			dfs(x+1);//下一个位置 
			chosen[i]=0;//回溯,还原情况 
		}
	}
}

int main(){
	dfs(1);
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/bingers/p/13197701.html