https://www.acmicpc.net/problem/15649
import Foundation
let input = readLine()!
let myinput: [Int] = input.components(separatedBy: " ").map(){Int($0)!}
let n = myinput[0]
let m = myinput[1]
var visited: [Bool] = [Bool](repeating: false, count: 8)
var result: [String] = [String](repeating: " ", count: 8)
func solution(_ depth: Int) {
if depth == m {
var temp: [String] = []
for i in 0..<depth {
temp.append(result[i])
}
print(temp.joined(separator: " "))
return
}
for i in 0..<n {
if visited[i] {
continue
}
visited[i] = true
result[depth] = "\(i+1)"
solution(depth + 1)
visited[i] = false
}
}
solution(0)
최대를 8로 주고 푸니까 출력이 내 눈에는 1 2 3 4 이렇게 보이는데
컴퓨터는 1 2 3 4 (빈값) (빈값) (빈값) (빈값) 이렇게 보이나부다
자꾸 출력 형식 잘못되었다고 ㅋㅋ..
그래서 출력용 배열 그때, 그때 만들어주고 결과 출력함. 잘 됨.
백트래킹 한번 더 공부해야할듯..?