初级8皇后问题,不是真正的8皇后问题,是该问题的初级条件限制:产生不同行,不同列的组合。
typedef struct Spoint { int x; int y; Spoint() { x = -1; y = -1; } }; static Spoint point[8]; int Queen8(int n, int cur) { static int iCount = 0; if(cur == n) { iCount++; for(int i = 0; i < n; i++) { printf("(%d,%d) ", point[i].x, point[i].y); } printf("\n"); } else { for(int i = 0; i < n; i++) { bool bNextRow = false; for(int j = 0; j < n; j++) { int bShow = false; for(int k = 0; k < cur; k++) { if(i == point[k].x ) { bNextRow = true; break; } if(j == point[k].y) { bShow = true; break; } } if(bNextRow) { bNextRow = false; break; } if(!bShow) { bShow = false; point[cur].x = i; point[cur].y = j; Queen8(A, n, cur + 1); } } } } return iCount; } int WQueen8_Train() { int cur = 0; int n = 8; int *B = (int *)malloc(n * sizeof(int)); memset(B, 0, n * sizeof(int)); int num = Queen8(n, cur); delete [] B; return num; }
原文:http://blog.csdn.net/weiqiwu1986/article/details/38850833