#include <cstdio> #include <vector> #include <algorithm> using namespace std; const int N = 8; vector<long> s; bool flag[N] = {false}; int a[N]; void help(int index) { if (index == N + 1) { for (int j = 1; j <= N; ++j) { for (int i = j + 1; i <= N; ++i) { if (abs(i - j) == abs(a[i] - a[j])) return; } } long temp = 0; for (int i = 1; i <= N; ++i) { temp = temp * 10 + a[i]; } s.push_back(temp); } for (int i = 1; i <= N; ++i) { if (!flag[i]) { flag[i] = true; a[index] = i; help(index + 1); flag[i] = false; } } } int main() { help(1); sort(s.begin(), s.end()); int n; while (scanf(" %d", &n) != EOF) { for (int i = 0; i < n; ++i) { int temp; scanf("%d", &temp); printf("%ld\n", s[temp - 1]); } } return 0; }
来源:https://www.cnblogs.com/ailinal/p/12315140.html