Question:



Follow up for N-Queens problem.


Now, instead outputting board configurations, return the total number of distinct solutions.




Anwser 1:

O(n^3)  based-on

Row



class Solution {
public:
    bool check(int row, int* colArray) {
        for (int i = 0; i < row; ++i) 
        {
            int diff = abs(colArray[i] - colArray[row]);      // in a col
            if (diff == 0 || diff == row - i) {         // int a row or line
                return false;
            }
        }
        return true;
    }

    void placeQueens(int row, int n, int &count, int* colArray) {
        if (row == n) {
            ++count;
            return;
        }
        
        for (int col = 0; col < n; ++col) {     // in 0 row, test n col
            colArray[row] = col;
            if (check(row, colArray)){
                placeQueens(row+1, n, count, colArray);    // test other rows
            }
        }
    }
    
    int totalNQueens(int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int *colArray = new int[n];
        int count = 0;
        
        placeQueens(0, n, count, colArray);
        
        return count;
    }
};




Anwser 2:



O(n^3)  based-on

Col



class Solution {
public:
    bool check(int col, int *rowArray){
        for(int i = 0; i < col; i++){
            if(rowArray[i] == rowArray[col] || abs(rowArray[i] - rowArray[col]) == col - i)
                return false;
        }
        return true;
    }
    
    void placeQueens(int col, int n, int &count, int *rowArray){
        if(col == n){
            count++;
            return;
        }
        
        for(int row = 0; row < n; row++){
            rowArray[col] = row;
            if(check(col, rowArray)){
                placeQueens(col+1, n, count, rowArray);
            }
        }
    }

    int totalNQueens(int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int *rowArray = new int[n];
        int count = 0;
        
        placeQueens(0, n, count, rowArray);
        
        return count;
    }
};



Anwser 3:



O(n^3)

while


    bool check(int row, int* colArray) {
        for (int i = 0; i < row; ++i) 
        {
            int diff = abs(colArray[i] - colArray[row]);        // in a col
            if (diff == 0 || diff == row - i) {                 // int a row or line
                return false;
            }
        }
        return true;
    }
    
    int totalNQueens(int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        if(n == 1) return 1;
        
        int *colArray = new int[n+1];
        int count = 0;
        
        colArray[1] = 0;
        int k = 1;
        while(k > 0){
            
            colArray[k] += 1;
            while( (colArray[k] <= n) && !check(k, colArray) ){
                colArray[k]++;
            }
            
            if(colArray[k] <= n){
                if(k == n){
                    count++;
                } else {
                    colArray[++k] = 0;
                } 
            } else {
                k--;
            }
        }
        
        return count;
    }


More Code:

g++


n-queue.c

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>

bool check(int row, int* colArray) {
    for (int i = 0; i < row; ++i) 
    {
        int diff = abs(colArray[i] - colArray[row]);        
        if (diff == 0 || diff == row - i) {                 
            return false;
        }
    }
    return true;
}

int totalNQueens(int n) {


    if(n == 1) return 1;

    int *colArray = new int[n+1];
    int count = 0;

    colArray[1] = 0;
    int k = 1;
    while(k > 0){

        colArray[k] += 1;
        while( (colArray[k] <= n) && !check(k, colArray) ){
            colArray[k]++;
        }

        if(colArray[k] <= n){
            if(k == n){
                count++;
            } else {
                colArray[++k] = 0;
            } 
        } else {
            k--;
        }
    }


    printf("%d\t%d\n", n, count);
    return count;
}

int main(){
    int num = 16;
    for(int i = 0; i < num; i++){
        totalNQueens(i);
    }

    return 0;
}


cmd:


g++ -g n-queue.c -o n-queue && ./n-queue


Result:


0


1

1


1

2


0

3


0

4


2

5


10

6


4

7


40

8


92

9


352

10


724

11


2680

12


14200

13


73712

14


365596

15


2279184



参考推荐:


N皇后问题


LeetCode题目:N-Queens