AcWing 843. n-皇后问题
原题链接
中等
作者:
yennywang
,
2024-11-04 20:49:58
,
所有人可见
,
阅读 1
样例
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let n = null
let path = [] //所有结果
let st = [] //记录dfs中当前数是否可用
rl.on('line', line => {
n = Number(line)
})
rl.on('close', line => {
dfs(0)
})
function dfs(u){
if(u === n){
console.log(path.join(' '))
return
}
for(let i = 1; i <= n; i++){
if(!st[i]){
path[u] = i
st[i] = true
dfs(u+1)
st[i] = false
}
}
}