Skip to content

Commit

Permalink
do : 2023-02-05 #28
Browse files Browse the repository at this point in the history
  • Loading branch information
HyomK committed Feb 6, 2023
1 parent b68c094 commit 1b59d01
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/kotlin/BOJ/back_tracking/9663.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package BOJ.back_tracking

fun main(){
P9663().solution()
}

class P9663 {
data class Pos(val row : Int, val col : Int)

var N = 0
var result = 0
var col = Array(15){0}

fun checker(cur : Pos) : Boolean{
//행탐색
for(i in 0 until cur.row){
if((col[i] == col[cur.row] || Math.abs(i-cur.row) == Math.abs(col[i]-cur.col))){
return false
}
}
return true
}

fun backTracking( level : Int){
//대각선과 각 row col에 놓아서는 안 된다
if(level == N ){
result ++
return
}
if(level>=N) return
for(j in 0 until N){
col[level] = j //level번째 행 j열 에 퀸을 놓는다.
if(checker(Pos(level,j))) {
//유망한 노드
backTracking(level+1)
}
}
}

fun solution()=with(System.`in`.bufferedReader()){
N = readLine().toInt()
backTracking(0)
println(result)
}
}

0 comments on commit 1b59d01

Please sign in to comment.