对列数(1-8)全排列,让每一行中的插入的数在列上没有重复
判断斜线方向是否存在多的皇后
#include <cstdio> #include <cmath> #include <iostream> using namespace std; int ans[10]; bool b[10]; void out() { int i,j; for(i = 1; i <= 7; i++) for(j = i+1; j <= 8; j++) if(i-j == -abs(ans[i]-ans[j])) return; for(i = 1; i <= 8; i++) cout << "(" << i << "," << ans[i] << ")" << endl; cout << endl; } void dfs(int num) { if(num >= 9) { out(); return ; } int i; for(i = 1; i <= 8; i++) { if(b[i] == false) { b[i] = true; ans[num] = i; dfs(num+1); /* for(int j = 1; j <= 8; j++) cout << j << ":" << ans[j] << endl; cout << endl; */ cout << num << " " << i << endl; ans[num] = 0; b[i] = false; } } } int main() { dfs(1); return 0; }