IT/알고리즘
[백준] N과 M
남갯
2024. 10. 30. 21:56
728x90
SMALL
https://www.acmicpc.net/problem/15649
https://veggie-garden.tistory.com/24
fun main() = with(Scanner(System.`in`)) {
val maxNumber = nextInt()
val count = nextInt()
val arr: Array<Int> = Array(maxNumber) { 0 }
val visit: Array<Boolean> = Array(count) { false }
dfs(maxNumber, count,0, arr, visit)
}
fun dfs(maxNumber: Int, count: Int, depth: Int, arr: Array<Int>, visit: Array<Boolean>) {
if(depth == count){
for(i in arr){
print("$i ")
}
println()
return
}
for (i in 0 until maxNumber) {
if(!visit[i]){
visit[i] = true
arr[depth] = i + 1
dfs(maxNumber, count, depth + 1 , arr, visit)
visit[i] = false
}
}
}
728x90
LIST