N皇后问题

采用的是构建树的方式,这个效率不高。

class Solution {
    struct Node {
        vector<int> queen;
        vector<vector<int>> pre;
        vector<string> result;
        Node* childs;
        int childnum;
    };
    void del(Node& head){
        for (int i = 0; i < head.childnum; ++i){
            del(head.childs[i]);
        }
        delete [] head.childs;
    }
    vector construct(struct Node& head, int dim){
        vector<int> index;
        for (int i = 0; i < head.queen.size(); ++i){
            if (head.pre[i].size() == 0 && head.queen[i] == 0){
                index.push_back(i);
            }
        }
        head.childs = new struct Node[index.size()];
        head.childnum = index.size();
        for (int i = 0; i < index.size(); ++i){
            head.childs[i].childnum = 0;
            head.childs[i].childs = NULL;
            head.childs[i].queen = head.queen;
            head.childs[i].queen[index[i]] = 1;
            head.childs[i].result = head.result;
            string newline(dim,'.');
            newline[index[i]] = 'Q';
            head.childs[i].result.push_back(newline);
            for (int t = 0; t < dim; ++t){
                head.childs[i].pre.push_back({});
            }
            for (int j = 0; j < head.pre.size(); ++j){
                for (int k = 0; k < head.pre[j].size(); ++k){
                    int temp = j + head.pre[j][k];
                    if (temp < 0 || temp >= dim){
                        continue;
                    }
                    else{
                        head.childs[i].pre[temp].push_back(head.pre[j][k]);
                    }
                }
            }
            if (index[i] + 1 < dim){
                head.childs[i].pre[index[i]+1].push_back(1);
            }
            if (index[i] - 1 >= 0){
                head.childs[i].pre[index[i]-1].push_back(-1);
            }
        }
        vector result = {};
        for (int i = 0; i < head.childnum; ++i){
            result.push_back(&(head.childs[i]));
        }
        return result;
    }
public:
    vector<vector<string>> solveNQueens(int dim) {
        Node* head = new struct Node;
        for (int i = 0; i < dim; ++i){
            head->queen.push_back(0);
            head->pre.push_back({});
        }
        head->result = {};
        head->childnum = 0;
        head->childs = NULL;
        vector result;
        vector temp1;
        vector temp2;
        result.push_back(head);
        for (int i = 0; i < dim; ++i){
            temp2.clear();
            for (int j = 0; j < result.size(); ++j){
                temp1 = construct(*(result[j]), dim);
                for (int k = 0; k < temp1.size(); ++k){
                    temp2.push_back(temp1[k]);
                }
            }
            result = temp2;
        }
        vector<vector<string>> all;
        for(int i = 0; i < result.size(); ++i){
            all.push_back(result[i]->result);
        }
        del(*head);
        delete head;
        return all;
    }
};

拜读的程序

class Solution {
public:
    vector<vector<string>> b;
    vector<vector<string>> solveNQueens(int n) {
        string s(n, ' ');
        vector<string> arr(n, s);
        if(n==1){
            arr[0][0]='Q';
            b.push_back(arr);
        }
        helper(0, n, arr);
        return b;
    }
    bool helper(int t, int n, vector<string>& ar) {
        for (int i = 0; i < n; i++) {
            if (ar[t][i] == ' ') {
                if (t == n - 1) {
                    ar[t][i] = 'Q';
                    b.push_back(ar);
                    return true;
                }
                int cha = 1, line = t + 1;
                vector<string> a = ar;
                a[t][i] = 'Q';
                int j = i + 1;
                while (j < n) {
                    a[t][j++] = '.';
                }
                while (line < n) {
                    a[line][i] = '.';
                    if (i - cha >= 0)    a[line][i - cha] = '.';
                    if (i + cha < n)     a[line][i + cha] = '.';
                    cha++;
                    line++;
                }
                helper(t + 1, n, a);
                ar[t][i] = '.';
            }
        }
        return false;
    }
};

你可能感兴趣的:(C++)