Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N皇后 #441

Open
Pcjmy opened this issue Jan 29, 2023 · 2 comments
Open

N皇后 #441

Pcjmy opened this issue Jan 29, 2023 · 2 comments

Comments

@Pcjmy
Copy link
Contributor

Pcjmy commented Jan 29, 2023

No description provided.

@Pcjmy
Copy link
Contributor Author

Pcjmy commented Jan 31, 2023

题目链接:https://leetcode.cn/problems/n-queens
时间复杂度:O(N!)

/**
 * @param {number} n
 * @return {string[][]}
 */
var solveNQueens = function(n) {
    let ans=[];
    let arr=new Array(n); // 存放解决方案
    let col=new Array(n); // 标记当前列有没有皇后
    let vis1=new Array(2*n); // 标记y=-x方向有没有皇后
    let vis2=new Array(2*n); // 标记y=x方向有没有皇后
    function dfs(x) {
        if(x===n) {
            let A=[];
            for(let i=0;i<n;i++) {
                let str='';
                for(let j=0;j<n;j++) {
                    if(arr[i]===j) {
                        str+='Q';
                    } else {
                        str+='.';
                    }
                }
                A.push(str);
            }
            ans.push(A);
            return ;
        }
        for(let i=0;i<n;i++) {
            let u=x+i;
            let v=x-i+n;
            if(!col[i]&&!vis1[u]&&!vis2[v])
            {
                col[i]=vis1[u]=vis2[v]=1;
                arr[x]=i;
                dfs(x+1);
                col[i]=vis1[u]=vis2[v]=0;
            }
        }
    }
    dfs(0);

    return ans;
};

@JsweetA
Copy link

JsweetA commented Oct 28, 2023

/**
 * @param {number} n
 * @return {string[][]}
 */
var solveNQueens = function (n) {
    const res = [];
    const arr = Array.from({ length: n }, () => new Array(n).fill('.'))
    const status = Array.from({ length: n }, () => new Array(n).fill(false))

    const check = (i, j, status) => {
        for (let a = i - 1, b = 1; a >= 0; a--, b++) {
            if (status[a][j]) return false;
            if (j + b < n && status[a][j + b]) return false;
            if (j - b >= 0 && status[a][j - b]) return false;
        }
        return true;
    }
    const dfs = (q) => {
        if (q === n) {
            res.push(arr.map(i => i.join('')))
        } else {
            for (let i = 0; i < n; i++) {
                if (check(q, i, status)) {
                    status[q][i] = true;
                    arr[q][i] = 'Q';
                    dfs(q + 1);
                    arr[q][i] = '.';
                    status[q][i] = false;
                }
            }
        }
    }
    dfs(0)
    return res;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants