Ejemplo n.º 1
0
 int totalNQueens(int n) {
     // IMPORTANT: Please reset any member data you declared, as
     // the same Solution instance will be reused for each test case.
     vector<int> row(n + 1);
     int sum = 0;
     putQueen(row, 1, n, sum);
     return sum;
 }
Ejemplo n.º 2
0
    int totalNQueens(int n) {

        result = 0;
        int *pos = new int[n];

        putQueen(pos, 0, n);
        delete []pos;

        return result;
    }
Ejemplo n.º 3
0
int main(){
    struct queen q;
    int n;
    printf("請輸入皇后數:");
    scanf("%d", &n);
    initQueen(&q, n);
    putQueen(&q, 1);
    printf("八皇后問題共有%d組解\n", q.solutions);
    return 0;
}
Ejemplo n.º 4
0
    void putQueen(int* pos, int line, int N){
        if(line >= N){
            takeRecord(pos, N);
            return;
        }

        for(int i=0; i<N; i++){
            pos[line] = i;
            if(valid(pos, line, N))
                putQueen(pos, line+1, N);
        }
    }
Ejemplo n.º 5
0
 void putQueen(vector<int> &queenPos, int row, int n, int &result) {
     if (row == n) {
         result++;
         return;
     }
     for (int col = 0; col < n; col++) {
         if (check(queenPos, row, col, n)) {
             queenPos[row] = col;
             putQueen(queenPos, row+1, n, result);
             queenPos[row] = -1;
         }
     }
 }
Ejemplo n.º 6
0
 void putQueen(vector<int> &row, int n, int total, int &sum) {
     int i, j;
     for (i = 1;i <= total;i++) {
         for (j = 1;j < n;j++)
             if (i == row[j] || n - j == abs(i - row[j]))
                 break;
         if (j == n) {
             if (n == total) {
                 sum++;
                 return;
             }
             row[n] = i;
             putQueen(row, n + 1, total, sum);
             row[n] = 0;
         }
     }
 }
Ejemplo n.º 7
0
void putQueen(struct queen *q, int row) {
    int i, j, pos;
    if (row > q->n) { //已經放n上皇后了
        q->solutions++;
        for (i = 1; i <= q->n; i++) {
            for (j = 1; j <= q->n; j++) {
	        if (q->board[i*(q->n+2)+j] == 'Q')
	            printf("后");
	        else
	            printf("口");
            } 
            printf("\n");
        }
        printf("\n");
        return;
    }
    //開始畫減, 再row列放上皇后
    for (i = 1; i <= q->n; i++) {
        //若此位置沒有其他皇后吃得到
        if(q->board[row*(q->n+2)+i] == 0) {
            //放上皇后
            q->board[row*(q->n+2)+i] = 'Q';
            //設定下方勢力範圍, 共有3個方向要設定
            for (j = 0; j < 3; j++) {
                for (pos = row*(q->n+2)+i+q->dir[j]; q->board[pos] >= 0; pos += q->dir[j]) {
                    q->board[pos]++;
                }
            }
            //開始recusion放下一個皇后
            putQueen(q, row+1);
            //回復原來的狀態
            //移除皇后
            q->board[row*(q->n+2)+i] = 0;
            //反射訂下方式一範圍共有三個方向要設定
            for (j = 0; j < 3; j++) {
                for (pos = row*(q->n+2)+i+q->dir[j]; q->board[pos] >= 0; pos += q->dir[j]) {
                    q->board[pos]--;
                }
            } 
        }
    }
}
Ejemplo n.º 8
0
 int totalNQueens(int n) {
     int result = 0;
     vector<int> queenPos(n, -1);
     putQueen(queenPos, 0, n, result);
     return result;
 }