Skip to content

Commit 2f397a1

Browse files
committed
백트래킹 업데이트
1 parent 0010f7f commit 2f397a1

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

part6/8.n-queen.txt

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const fs = require("fs")
2+
const input = fs.readFileSync("index.txt").toString().split("\n")
3+
4+
let n=Number(input[0])
5+
6+
7+
let cnt=0;
8+
let result=[]
9+
10+
11+
function isPossible(row,i){
12+
for(let [x,y]of result){
13+
if(row===x){
14+
return false
15+
}
16+
if(i===y){
17+
return false
18+
}
19+
if(Math.abs(row-x)===Math.abs(i-y)){
20+
return false
21+
}
22+
}
23+
return true
24+
}
25+
26+
function dfs(row){
27+
if(row===n){
28+
29+
cnt++
30+
}else {
31+
for(let i=0;i<n;i++){
32+
if(!isPossible(row,i)){
33+
continue
34+
}
35+
36+
result.push([row,i])
37+
dfs(row+1)
38+
result.pop()
39+
}
40+
41+
}
42+
43+
}
44+
dfs(0)
45+
46+
console.log(cnt)

0 commit comments

Comments
 (0)