[Coding Test]/[백준]
[백준] 13023 파이썬(python) : ABCDE - (★)
13023번 : ABCDE import sys n, m = map(int, sys.stdin.readline().split()) graph = [ [] for _ in range(n+1) ] for _ in range(m): a, b = map(int, sys.stdin.readline().split()) graph[a].append(b) graph[b].append(a) visit = [False] * (n+1) res = False def dfs(idx, depth): global res if depth == 4: #1 res = True return for i in graph[idx]: if not visit[i]: visit[i] = True dfs(i, depth+1) visit[i] = Fal..
[백준] 15666 파이썬(python) : N과 M (12)
15666번 : N과 M (12) import sys n, m = map(int, sys.stdin.readline().split()) number = list(map(int, sys.stdin.readline().split())) number.sort() res = [] def backTracking(depth): if depth == m: print(*res) return overlap = 0 #1 for i in range(len(number)): if res and res[-1] > number[i]: continue if overlap != number[i]: #2 overlap = number[i] res.append(number[i]) backTracking(depth+1) res.pop()..
[백준] 15665 파이썬(python) : N과 M (11)
15665번 : N과 M (11) import sys n, m = map(int, sys.stdin.readline().split()) number = list(map(int, sys.stdin.readline().split())) number.sort() res = [] def backTracking(depth): if depth == m: print(*res) return overlap = 0 for i in range(len(number)): if overlap != number[i]: overlap = number[i] res.append(number[i]) backTracking(depth+1) res.pop() backTracking(0) 중복을 허용하고 자신보다 낮은 숫자가 다음에 와도 된다..
[백준] 15664 파이썬(python) : N과 M (10) - (★)
15664번 : N과 M (10) import sys n, m = map(int, sys.stdin.readline().split()) number = list(map(int, sys.stdin.readline().split())) number.sort() res = [] visit = [False]*10001 def backTracking(depth): if depth == m: print(*res) return overlap = 0 for i in range(len(number)): if len(res) != 0 and res[-1] > number[i]: #1 continue if not visit[i] and overlap != number[i]: overlap = number[i] visit[i..
[백준] 15663 파이썬(python) : N과 M (9) - (★)
15663번 : N과 M (9) import sys n, m = map(int, sys.stdin.readline().split()) number = list(map(int, sys.stdin.readline().split())) number.sort() res = [] visit = [False]*10001 def backTracking(depth): if depth == m: print(*res) return overlap = 0 #1 for i in range(len(number)): #2 if not visit[i] and overlap != number[i]: res.append(number[i]) visit[i] = True overlap = number[i] #3 backTracking(de..
[백준] 15657 파이썬(python) : N과 M (8)
15657번 : N과 M (8) n, m = map(int, input().split()) answer = list(map(int, input().split())) answer.sort() result = [] def dfs(depth): if depth == m: print(*result) return for ans in answer: if len(result) != 0 and result[-1] > ans: #1 continue result.append(ans) dfs(depth+1) result.pop() dfs(0) #1 : 문제의 패턴을 보면 자기보다 작은 수는 오른쪽에 올 수 없다. 그래서 result의 길이가 0이 아닐때 가장 오른쪽 원소보다 새로들어올 숫자가 작으면 continue 해준다...