From 1b59d016d7448a6b76319a74d53a3816fe5a39d3 Mon Sep 17 00:00:00 2001 From: Hyomk Date: Sun, 5 Feb 2023 22:19:19 +0900 Subject: [PATCH] do : 2023-02-05 #28 --- src/main/kotlin/BOJ/back_tracking/9663.kt | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/kotlin/BOJ/back_tracking/9663.kt diff --git a/src/main/kotlin/BOJ/back_tracking/9663.kt b/src/main/kotlin/BOJ/back_tracking/9663.kt new file mode 100644 index 0000000..397791a --- /dev/null +++ b/src/main/kotlin/BOJ/back_tracking/9663.kt @@ -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) + } +} \ No newline at end of file