#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #define ci cin.tie(0) #define ios ios::sync_with_stdio(false) #define fi first #define se second using namespace std; typedef long long LL; typedef pair<int, int> PII; const int N = 22; int n, ans; int a[N]; bool vis[N]; bool check(int num, int k) { if (k == 1 || k == 2) return true; if (a[k - 1] > a[k - 2] && num < a[k - 2]) return true; if (a[k - 1] < a[k - 2] && num > a[k - 2]) return true; return false; } void dfs(int u) { if (u > n) return ; for (int i = 1; i <= n; i ++ ) { if (!vis[i] && check(i, u)) { vis[i] = true; a[u] = i; if (u >= 2) { ans ++ ; /* for (int i = 1; i <= u; i ++ ) cout << a[i] << ‘ ‘; cout << endl; */ } dfs(u + 1); vis[i] = false; } } } int main() { ci;ios; cin >> n; dfs(1); cout << ans << endl; return 0; }
原文:https://www.cnblogs.com/zbx2000/p/12710300.html