20250825 baekjoon
This commit is contained in:
parent
d50679b5cf
commit
78c684d640
26
code_study/Baekjoon/python/15663.py
Normal file
26
code_study/Baekjoon/python/15663.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
input = sys.stdin.readline
|
||||||
|
|
||||||
|
def dfs(n, m, nums, visited, result, result_set) :
|
||||||
|
if len(result) == m :
|
||||||
|
result_set.append(result.copy())
|
||||||
|
return result_set
|
||||||
|
|
||||||
|
for i in range(n) :
|
||||||
|
if not visited[i] :
|
||||||
|
visited[i] = True
|
||||||
|
result.append(nums[i])
|
||||||
|
result_set = dfs(n,m,nums,visited,result, result_set)
|
||||||
|
result.pop()
|
||||||
|
visited[i] = False
|
||||||
|
|
||||||
|
return result_set
|
||||||
|
|
||||||
|
N, M = map(int, input().split())
|
||||||
|
nums = list(map(int,input().split()))
|
||||||
|
visited = [False]*N
|
||||||
|
|
||||||
|
result_set = sorted(list(set(tuple(t) for t in dfs(N,M,nums,visited,[],[]))))
|
||||||
|
for t in result_set :
|
||||||
|
print(*t)
|
||||||
29
code_study/Baekjoon/swift/15663.swift
Normal file
29
code_study/Baekjoon/swift/15663.swift
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
func dfs(_ n: Int, _ m: Int, _ nums: [Int], _ visited: inout [Bool], _ result: inout [Int]) {
|
||||||
|
if result.count == m {
|
||||||
|
print(result.map{String($0)}.joined(separator: " "))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var prev = -1
|
||||||
|
for i in 0..<n {
|
||||||
|
if !visited[i] && nums[i] != prev {
|
||||||
|
visited[i] = true
|
||||||
|
result.append(nums[i])
|
||||||
|
dfs(n,m,nums,&visited,&result)
|
||||||
|
_ = result.popLast()
|
||||||
|
visited[i] = false
|
||||||
|
prev = nums[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let input1 = readLine(), let NM = input1.split(separator: " ").map({Int($0)}) as? [Int], NM.count == 2 {
|
||||||
|
let N = NM[0]
|
||||||
|
let M = NM[1]
|
||||||
|
|
||||||
|
if let input2 = readLine(), let nums = input2.split(separator: " ").map({Int($0)}) as? [Int], nums.count == N {
|
||||||
|
var visited:[Bool] = Array(repeating: false, count: N)
|
||||||
|
var result: [Int] = []
|
||||||
|
dfs(N, M, nums.sorted(), &visited, &result)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user