标签:
# include <iostream> using namespace std; // 超时,但是只有十个数字 int main() { int n; int a[11] = {0, 1, 0, 0, 2, 10, 4, 40, 92, 352, 724}; while(cin >> n) { if(n == 0) break; cout << a[n] << endl; } return 0; }
超时代码:
#include<iostream> #include<fstream> using namespace std; int a[16]; int n, sum; bool f(int x) { for(int i = 1; i < x; i++) { if(abs(x - i) == abs(a[x] - a[i]) || a[x] == a[i]) return false; } return true; } int dfs(int x) { if(x > n && n > 0) sum += 1; else { for(int i = 1; i <= n; i++) { a[x] = i; if(f(x)) dfs(x + 1); } } return sum; } int main() { //fstream cin("aaa.txt"); while(cin >> n) { if(n == 0) break; sum = 0; cout << dfs(1) << endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5401980.html