문제풀이/백트래킹

https://www.acmicpc.net/problem/1497코드n, m = map(int, input().split())guitars = []maximum_song = 0for _ in range(n): name, songs = input().split() binary = "" for song in songs: binary += "1" if song == "Y" else "0" guitars.append((name, int(binary, 2))) maximum_song |= int(binary, 2)answer = int(1e9)def bt(idx, guitar_cnt, song): global answer if idx == n or guitar_c..
https://www.acmicpc.net/problem/1248코드n = int(input())input_data = "_"+input()sign_sequence = [[None]*(n+1) for _ in range(n+1)]idx = 1for i in range(1, n+1): for j in range(i, n+1): sign_sequence[i][j] = input_data[idx] idx += 1def bt(idx, arr, acc): if idx == n+1: print(*arr[1:]) exit() for new_num in range(-10, 11): new_acc = acc[-1]+new_num ..
https://www.acmicpc.net/problem/1759 이 문제는 백트래킹과 조합 두 가지 방법으로 풀 수 있다.코드(백트래킹)l, c = map(int, input().split())alphabets = sorted(list(input().split()))def check_validation(str): vowels = {"a", "e", "i", "o", "u"} vowel_cnt = 0 for i in range(len(str)): if str[i] in vowels: vowel_cnt += 1 if vowel_cnt >= 1 and len(str)-vowel_cnt >= 2: return True else: ..
https://www.acmicpc.net/problem/6987코드from itertools import combinationsdef bt(idx): global ans, match_results if idx == 15: ans = 1 for nation in match_results: if nation.count(0) != 3: ans = 0 break return home, away = matchs[idx] for h, w in [(0, 2), (1, 1), (2, 0)]: # home 이김, 비김, home 짐 if match_results[home]..
https://www.acmicpc.net/problem/7490 코드ans = []def bt(n, idx, exp): if idx == n: exp += str(idx) if eval(exp.replace(" ", '')) == 0: ans.append(exp) return for mid in ['+', '-', ' ']: bt(n, idx+1, exp+str(idx)+mid)for _ in range(int(input())): n = int(input()) ans = [] # 정답 배열 초기화 bt(n, 1, "") ans.sort() # ASCII 순서로 정렬 for i in rang..
딜레이레이
'문제풀이/백트래킹' 카테고리의 글 목록