CodeVs 1295 N皇后问题
题目大意:
http://codevs.cn/problem/1295/
代码:
#include <iostream> #include <cmath> using namespace std; int n; int arr[15] = {0}; int count = 0; bool canPos(int x) { for(int i = 1; i < x; i++) { if(arr[i] == arr[x] || abs(arr[i]-arr[x]) == abs(x-i)) return false; } return true; } void dfs(int x) { if(x == n+1) { count++; } for(int i = 1; i <= n; i++) { arr[x] = i; if(canPos(x)) dfs(x+1); } } int main() { cin >> n; dfs(1); cout << count << endl; return 0; }