반응형

백트래킹 7

[파이썬, Python] 백준 6603: 로또

문제 https://www.acmicpc.net/problem/6603 6603번: 로또 입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로 www.acmicpc.net 코드 from collections import deque while True: nums = deque(map(int, input().split())) k = nums.popleft() if k == 0: break s = [] def dfs(): if len(s) == 6: print(' '.join(map(str,s))) return for i in sorted(nums): i..

[파이썬, Python] 백준 15655: N과 M (6)

문제 https://www.acmicpc.net/problem/15655 15655번: N과 M (6) N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 www.acmicpc.net 코드 n, m = map(int, input().split()) nums = sorted(list(map(int, input().split()))) s = [] def dfs(start): if len(s) == m: print(' '.join(map(str,s))) return for i in range(start, len(nums)): if nums[i] not in s: s.a..

[파이썬, Python] 백준 15654: N과 M (5)

문제 https://www.acmicpc.net/problem/15654 15654번: N과 M (5) N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 www.acmicpc.net 코드 n, m = map(int, input().split()) nums = list(map(int, input().split())) s = [] def dfs(): if len(s) == m: print(' '.join(map(str,s))) return for i in sorted(nums): if i not in s: s.append(i) dfs() s.pop() dfs() 설..

[파이썬, Python] 백준 10974: 모든 순열

문제 https://www.acmicpc.net/problem/10974 10974번: 모든 순열 N이 주어졌을 때, 1부터 N까지의 수로 이루어진 순열을 사전순으로 출력하는 프로그램을 작성하시오. www.acmicpc.net 코드 n = int(input()) s = [] def dfs(): if len(s) == n: print(' '.join(map(str,s))) return for i in range(1, n+1): if i not in s: s.append(i) dfs() s.pop() dfs() 설명 https://looancheong.tistory.com/147 [파이썬, Python] 백준 15649: N과 M (1) 문제 https://www.acmicpc.net/problem/1564..

[파이썬, Python] 백준 15652: N과 M (4)

문제 https://www.acmicpc.net/problem/15652 15652번: N과 M (4) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net 코드 import sys input = sys.stdin.readline n, m = map(int, input().split()) s = [] def dfs(): if len(s) == m: print(' '.join(map(str,s))) return for i in range(1, n+1): if s: if s[-1] > i: continue s.append(i) dfs() s.pop..

[파이썬, Python] 백준 15651: N과 M (3)

문제 https://www.acmicpc.net/problem/15651 15651번: N과 M (3) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net 코드 n, m = map(int, input().split()) s = [] def dfs(): if len(s) == m: print(' '.join(map(str,s))) return for i in range(1, n+1): s.append(i) dfs() s.pop() dfs() 설명 앞서 풀었던 문제와 굉장히 유사하다. https://looancheong.tistory.com/14..

[파이썬, Python] 백준 15649: N과 M (1)

문제 https://www.acmicpc.net/problem/15649 15649번: N과 M (1) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net 코드 n, m = map(int, input().split()) s = [] def dfs(): if len(s) == m: print(' '.join(map(str,s))) return for i in range(1, n+1): if i not in s: s.append(i) dfs() s.pop() dfs() 설명 처음에는 간단한 다중 반복문으로 구성하려고 했으나 수가 커질수록 불가능에..

반응형