dfs硬解字典序全RE样例能过,求调!
查看原帖
dfs硬解字典序全RE样例能过,求调!
940472
Sirenaya楼主2024/3/23 09:26
#include <bits/stdc++.h> 
using namespace std;
#define ll long long
bool num[15] = { false };
ll n,used[15],ans=0;
string s;
void check()
{
	for (int i = 1; i <= n; i++)
		if (used[i] != s[i - 1] - '0') return;
	cout << ans << endl;
	exit(-1);
}
void dfs(int x) {
	if (x > n) {
		ans++;
		check();
		return;
	}
	for (int i = 1; i <= n; i++) {
		if (num[i] == false) {
			
			num[i] = true;
			used[x] = i;
			dfs(x + 1);
			num[i] = false; //回溯
		}
	}
}
int main() {
	cin >> n;
	cin >> s;
	dfs(1);
	return 0;
}
2024/3/23 09:26
加载中...