NC15128 老子的全排列呢 dfs 全排列 (何以解忧,唯有暴搜)

链接:https://ac.nowcoder.com/acm/problem/15128
来源:牛客网

题目描述
老李见和尚赢了自己的酒,但是自己还舍不得,所以就耍起了赖皮,对和尚说,光武不行,再来点文的,你给我说出来1-8的全排序,我就让你喝,这次绝不耍你,你能帮帮和尚么?
输入描述:

输出描述:

1~8的全排列,按照全排列的顺序输出,每行结尾无空格。

示例1
输入
复制

No_Input

输出
复制

Full arrangement of 1~8

备注:

1~3的全排列 :
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1


求全排列是dfs的基本功了

#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN (16)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#ifdef debug
#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#endif

#ifndef debug
namespace FIO {
	template <typename T>
	void read(T& x) {
		int f = 1; x = 0;
		char ch = getchar();

		while (ch < '0' || ch > '9') 
			{ if (ch == '-') f = -1; ch = getchar(); }
		while (ch >= '0' && ch <= '9') 
			{ x = x * 10 + ch - '0'; ch = getchar(); }
		x *= f;
	}
};
using namespace FIO;
#endif


int n = 8, m, Q, K, rs[MAXN], a[MAXN], vis[MAXN];

void dfs(int level) {
	if(level == n+1) {
		for(int i=1; i<=n; i++)
			printf("%d%c", a[rs[i]], i==n?'\n':' ');
		return ;
	}
	for(int i=1; i<=n; i++) {
		if(!vis[i]) {
			vis[i] = true;
			rs[level] = i;
			dfs(level+1);
			vis[i] = false;
		}
	}
}

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	fori(1, 8) a[i] = i;
	dfs(1);





#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}




全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 听劝,这个简历怎么改 #
14099次浏览 183人参与
# 面试被问“你的缺点是什么?”怎么答 #
6438次浏览 100人参与
# 水滴春招 #
16521次浏览 349人参与
# 入职第四天,心情怎么样 #
11321次浏览 63人参与
# 租房找室友 #
8027次浏览 53人参与
# 读研or工作,哪个性价比更高? #
26163次浏览 356人参与
# 职场新人生存指南 #
199236次浏览 5510人参与
# 参加完秋招的机械人,还参加春招吗? #
27018次浏览 276人参与
# 文科生还参加今年的春招吗 #
4118次浏览 31人参与
# 简历无回复,你会继续海投还是优化再投? #
48629次浏览 561人参与
# 你见过最离谱的招聘要求是什么? #
144723次浏览 829人参与
# 如果重来一次你还会读研吗 #
155719次浏览 1706人参与
# 机械人选offer,最看重什么? #
69077次浏览 449人参与
# 选择和努力,哪个更重要? #
44310次浏览 493人参与
# 如果再来一次,你还会学硬件吗 #
103647次浏览 1245人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
20521次浏览 414人参与
# 招聘要求与实际实习内容不符怎么办 #
46753次浏览 494人参与
# 22届毕业,是读研还是拿外包offer先苟着 #
4652次浏览 27人参与
# 你们的毕业论文什么进度了 #
901291次浏览 8961人参与
# 软开人,你觉得应届生多少薪资才算合理? #
81379次浏览 496人参与
# 国企还是互联网,你怎么选? #
109198次浏览 853人参与
牛客网
牛客企业服务