前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【leetcode】N-Queens II

【leetcode】N-Queens II

作者头像
阳光岛主
发布2019-02-19 11:33:49
3840
发布2019-02-19 11:33:49
举报
文章被收录于专栏:米扑专栏米扑专栏

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
    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

代码语言:javascript
复制
#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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013年04月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档