Skip to content

Commit

Permalink
Merge pull request #192 from AlgoLeadMe/24-SeongHoonC
Browse files Browse the repository at this point in the history
24-SeongHoonC
  • Loading branch information
SeongHoonC authored May 23, 2024
2 parents d12c272 + 982369a commit 93acc1f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion SeongHoonC/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
| 20μ°¨μ‹œ | 2024.04.07 | DP | <a href="https://www.acmicpc.net/problem/7579">μ•±</a> |https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/75 |
| 21μ°¨μ‹œ | 2024.04.11 | DP | <a href="https://www.acmicpc.net/problem/1149">RGB거리</a> |https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/77 |
| 22μ°¨μ‹œ | 2024.05.01 | DP | <a href="https://www.acmicpc.net/problem/11053">κ°€μž₯ κΈ΄ μ¦κ°€ν•˜λŠ” λΆ€λΆ„ μˆ˜μ—΄</a> |https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/80 |
| 23μ°¨μ‹œ | 2024.05.14 | ??? | <a href="https://www.acmicpc.net/problem/1644">μ†Œμˆ˜μ˜ 연속합</a> |https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/186 |
| 23μ°¨μ‹œ | 2024.05.14 | μ†Œμˆ˜ | <a href="https://www.acmicpc.net/problem/1644">μ†Œμˆ˜μ˜ 연속합</a> |https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/186 |
| 24μ°¨μ‹œ | 2024.05.18| λ°±νŠΈλž˜ν‚Ή | <a href="https://www.acmicpc.net/problem/9663">n-queen</a> |https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/192 |
---
41 changes: 41 additions & 0 deletions SeongHoonC/dfs/nqeen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.io.BufferedReader
import java.io.InputStreamReader

var answer = 0
var n: Int = 0
lateinit var board: IntArray
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
n = br.readLine().toInt()
board = IntArray(n)
backTracking(0)
println(answer)
}

private fun backTracking(row: Int) {
// row κ°€ μ¦κ°€ν•˜λ‹€κ°€ n μ΄λž‘ 같아지면 μ •λ‹΅ +1
if (row == n) {
answer++
return
}
// row 일 λ•Œ 각 열에 놓을 경우의 수
for (col in 0 until n) {
board[row] = col
// 놓을 수 μžˆλ‹€λ©΄ λ‹€μŒ ν–‰
if (check(row)) backTracking(row + 1)
}
}

private fun check(row: Int): Boolean {
for (i in 0 until row) {
// 행이 κ°™κ±°λ‚˜
if (board[row] == board[i]) {
return false
}
// λŒ€κ°μ„ 
if (row - i == kotlin.math.abs(board[row] - board[i])) {
return false
}
}
return true
}

0 comments on commit 93acc1f

Please sign in to comment.