728x90
반응형

Algorithm/BackTracking 19

[Baekjoon 6603 / Python / 실버2] 로또

기본적인 백트래킹 문제이다.주어진 집합에서 6개 숫자의 조합(순서가 다른 같은 숫자를 중복으로 간주)을 구해야 한다. # 실버2 -> 백트래킹import sysinput = sys.stdin.readlinedef back_tracking(depth, start, result):    # 6개의 숫자가 모이면 출력    if depth == 6:        print(*result)        return        for i in range(start, len(S)):        result.append(S[i])        back_tracking(depth + 1, i + 1, result)        result.pop()while True:    K, *S = map(int, input..

[Baekjoon 1987 / Python / 골드4] 알파벳

DFS, 백트래킹 문제이다. 상하좌우 탐색을 요하기 때문에 BFS로도 잠시 생각했었으나 백트래킹이 필요했기에 DFS로 진행했다. 어느정도 정석에 가까운 백트래킹 문제 같다. # DFS, 백트래킹import sysinput = sys.stdin.readline# 입력받기R, C = map(int, input().split())board = []for _ in range(R):    board.append(list(input().strip()))# 사전준비directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]visited = [[False] * C for _ in range(R)]visited[0][0] = Truepath = set() # 집합으로 불필요한 중복 알파벳이 들어..

[Baekjoon 15666 / Python / 실버2] N과 M (12)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()result = [-1] * Mdef compare(a, b):    if a > b:        return False    return Truedef backTracking(depth):    if depth == M:        print(' '.join(map(str, result)))        return        temp = 0    for i in range(N):        if temp != numbers[i] and (depth == 0 or compare(..

[Baekjoon 15665 / Python / 실버2] N과 M (11)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()result = [-1] * Mdef backTracking(depth):    if depth == M:        print(' '.join(map(str, result)))        return        temp = 0    for i in range(N):        if temp != numbers[i]:            temp = numbers[i]            result[depth] = numbers[i]            backTracking(d..

[Baekjoon 15664 / Python / 실버2] N과 M (10)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()visited = [False] * Nresult = [-1] * Mdef compare(a, b):    if a > b:        return False    return Truedef backTracking(depth):    if depth == M:        print(' '.join(map(str, result)))        return        temp = 0    for i in range(N):        if (depth == 0 and temp != n..

[Baekjoon 15663 / Python / 실버2] N과 M (9)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())visited = [False] * Nnumbers = list(map(int, input().split()))numbers.sort()result = [-1] * Mdef backTracking(depth):    if depth == M:        print(' '.join(map(str, result)))        return        # temp 변수를 활용하여 완성된 중복 순열을 방지한다.    temp = 0     for i in range(N):        if not visited[i] and temp != numbers[i]:            resu..

[Baekjoon 15657 / Python / 실버3] N과 M (8)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()box = []def compare(a, b):    if a > b:        return False    return Truedef backTracking(depth):        if depth == M:        print(' '.join(map(str, box)))        return        for i in range(N):        if depth == 0 or compare(box[depth-1], numbers[i]):            box.ap..

[Baekjoon 15656 / Python / 실버3] N과 M (7)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()box = []def backTracking(depth):    if depth == M:        print(' '.join(map(str, box)))        return        for i in range(N):        box.append(numbers[i])        backTracking(depth + 1)        box.pop()backTracking(0) 백트래킹( 사실 백트래킹도 아니다 )을 통해 모든 수열을 만들어 출력하기만 하면 된다. 중복 수..

[Baekjoon 15655 / Python / 실버3] N과 M (6)

import sysinput = sys.stdin.readline# 중복 없으며 뒷 숫자가 더 커야 한다N, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()box = []def compare(a, b):    if a >= b:        return False    return Truedef backTracking(depth):    if depth == M:        print(' '.join(map(str, box)))        return        for i in range(N):        if depth == 0 or (numbers[i] not in box and compar..

[Baekjoon 15654 / Python / 실버3] N과 M (5)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numList = list(map(int, input().split()))numList.sort()box = []def backTracking(depth):    if depth == M:        print(' '.join(map(str, box)))        return        for i in range(N):        if not numList[i] in box:            box.append(numList[i])            backTracking(depth + 1)            box.pop()backTracking(0) 앞선 N과 M ..

728x90
반응형