백트래킹 31

[백준] 12100. 2048(Easy)

https://acmicpc.net/problem/12100 12100번: 2048 (Easy) 첫째 줄에 보드의 크기 N (1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 게임판의 초기 상태가 주어진다. 0은 빈 칸을 나타내며, 이외의 값은 모두 블록을 나타낸다. 블록에 쓰여 있는 수는 2 www.acmicpc.net from copy import deepcopy def backtrack(arr, index=5, dir_arr=[]): if index == 0: global max_val tmp = findmax(arr) if max_val < tmp: max_val = tmp return else: for i in range(4): tmp_arr = deepcopy(arr) tmp_arr..

STUDY/Algorithm 2021.03.16

[백준, SWEA] BOJ 14889 스타트와 링크, swea4012 요리사

https://acmicpc.net/problem/14889 14889번: 스타트와 링크 예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다. www.acmicpc.net n = int(input()) s_ij = [list(map(int, input().split())) for i in range(n)] min_val = 100 * 20 visit = [0] * n def comb(index=0, arr=[]): if index == (n // 2): global min_val value = total_chk(arr) if min_val > value: min_val = value else:..

STUDY/Algorithm 2021.03.10

[백준] 14888 연산자 끼워넣기

https://acmicpc.net/problem/14888 14888번: 연산자 끼워넣기 첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, www.acmicpc.net n = int(input()) n_list = list(map(int, input().split())) operator = list(map(int, input().split())) min_val = 1000000000 max_val = -1000000000 def backtrack(index, number=n_list[0]): if index == ..

STUDY/Algorithm 2021.03.10

[백준] 9663 N-Queen

https://acmicpc.net/problem/9663 9663번: N-Queen N-Queen 문제는 크기가 N × N인 체스판 위에 퀸 N개를 서로 공격할 수 없게 놓는 문제이다. N이 주어졌을 때, 퀸을 놓는 방법의 수를 구하는 프로그램을 작성하시오. www.acmicpc.net n = int(input()) matrix = [[0] * n for _ in range(n)] result = 0 # 1: 퀸, 0: 놓을수 있는 자리, -1: 놓을수 없는 자리 def backtrack(index, arr=[]): if index == n: global result result += 1 else: for i in range(n): # 한줄에서 하나씩만 보면 됨 if i in arr: continue ..

STUDY/Algorithm 2021.03.10

[백준] 2580 스도쿠

https://acmicpc.net/problem/2580 2580번: 스도쿠 스도쿠는 18세기 스위스 수학자가 만든 '라틴 사각형'이랑 퍼즐에서 유래한 것으로 현재 많은 인기를 누리고 있다. 이 게임은 아래 그림과 같이 가로, 세로 각각 9개씩 총 81개의 작은 칸으로 이루 www.acmicpc.net import sys input = sys.stdin.readline sudoku = [] zero_list = [] for i in range(9): tmp = list(map(int, input().split())) sudoku.append(tmp) for j in range(9): if tmp[j]: continue zero_list.append((i,j)) def backtrack(index): i..

STUDY/Algorithm 2021.03.09

[백준] 1759 암호만들기

www.acmicpc.net/problem/1759 1759번: 암호 만들기 첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다. www.acmicpc.net L, C = map(int, input().split()) char = input().split() char.sort() def comb(index=0, arr=[], limit=L): if index == limit: vowel = {'a','e','i','o','u'} vow, con = 0, 0 for i in range(limit): if arr[i] in vowel: vow += 1 else: con += ..

STUDY/Algorithm 2021.03.05

[백준] 3980 선발명단

www.acmicpc.net/problem/3980 3980번: 선발 명단 각각의 테스트 케이스에 대해서, 모든 포지션의 선수를 채웠을 때, 능력치의 합의 최댓값을 출력한다. 항상 하나 이상의 올바른 라인업을 만들 수 있다. www.acmicpc.net for tc in range(int(input())): player = [0] * 11 for i in range(11): player[i] = list(map(int,input().split())) visit = [0] * 11 max_val = 0 def comb(index=0, total=0): if index == 11: global max_val if max_val < total: max_val = total else: for i in range..

STUDY/Algorithm 2021.03.05

[백준] 6603 로또

www.acmicpc.net/problem/6603 6603번: 로또 입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로 www.acmicpc.net 로또 1등 한번만 받아보고싶습니다..... while True: k, *S = map(int, input().split()) if k == 0: break visited = [0] * k def lotto(index=0, arr=[]): if index == 6: print(*arr) return else: for i in range(index, k): if visited[i] == 0: if arr..

STUDY/Algorithm 2021.03.04

[백준] N과 M (시리즈)

www.acmicpc.net/workbook/view/2052 문제집: N과 M (시리즈) www.acmicpc.net 백트래킹을 연습하기 위해 4번까지 풀었다. 15649 n, m = map(int, input().split()) visited = [0] * (n + 1) def asdf(index=0, arr=[], key=m, end=n): if index == key: print(' '.join(map(str, arr))) else: for i in range(1,end+1): if visited[i] == 0: visited[i] = 1 asdf(index + 1, arr + [i]) visited[i] = 0 asdf() 15650 n, m = map(int, input().split()) d..

STUDY/Algorithm 2021.03.04

[백준] 2798 블랙잭

www.acmicpc.net/problem/2798 2798번: 블랙잭 첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장 www.acmicpc.net n, m = map(int, input().split()) cards = list(map(int, input().split())) max_val = 0 def blackjack(index=0, visit_index=-1, arr=[], length=n, key=m): if index == 3: if sum(arr) > key: return global max_val if max_..

STUDY/Algorithm 2021.03.04