Baekjoon 150

[백준 5052번] 전화번호 목록

5052번: 전화번호 목록 첫째 줄에 테스트 케이스의 개수 t가 주어진다. (1 ≤ t ≤ 50) 각 테스트 케이스의 첫째 줄에는 전화번호의 수 n이 주어진다. (1 ≤ n ≤ 10000) 다음 n개의 줄에는 목록에 포함되어 있는 전화번호가 www.acmicpc.net import sys input = sys.stdin.readline t = int(input().rstrip()) for _ in range(t): n = int(input().rstrip()) cases = [] flag = True for _ in range(n): cases.append(input().rstrip()) cases.sort() # for i in range(len(cases)): # if flag: # break # f..

Algorithm/String 2023.12.13

[백준 1941번] 소문난 칠공주

1941번: 소문난 칠공주 총 25명의 여학생들로 이루어진 여학생반은 5×5의 정사각형 격자 형태로 자리가 배치되었고, 얼마 지나지 않아 이다솜과 임도연이라는 두 학생이 두각을 나타내며 다른 학생들을 휘어잡기 시작 www.acmicpc.net from itertools import combinations from collections import deque dx = [-1,1,0,0] dy = [0,0,-1,1] area = [list(input()) for _ in range(5)] positions = [] for i in range(5): for j in range(5): positions.append((i,j)) combs = list(combinations(positions,7)) answer ..

[백준 7682번] 틱택토

7682번: 틱택토 입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 줄은 9개의 문자를 포함하며, 'X', 'O', '.' 중 하나이다. '.'은 빈칸을 의미하며, 9개의 문자는 게임판에서 제일 윗 줄 왼쪽부터의 순서이다. 입 www.acmicpc.net area = [["."]*3 for _ in range(3)] cases = set() def check_is_full(): is_full = True for i in range(3): for j in range(3): if area[i][j] == '.': is_full = False return is_full def check_end(): if check_is_full(): return True if (area[0][0] == area[0][1..

[백준 1182번] 부분수열의 합

1182번: 부분수열의 합 첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다. www.acmicpc.net n, s = map(int,input().split()) # N개의 정수로 이루어진 수열, 부분수열의 원소를 다 더한 값이 S가 되는 경우의 수 data = list(map(int,input().split())) count = 0 visited = [False]*n def dfs(total,index,depth): global count if total == s and depth != 0: count += 1 for i in ..

[백준 2589번] 보물섬

2589번: 보물섬 보물섬 지도를 발견한 후크 선장은 보물을 찾아나섰다. 보물섬 지도는 아래 그림과 같이 직사각형 모양이며 여러 칸으로 나뉘어져 있다. 각 칸은 육지(L)나 바다(W)로 표시되어 있다. 이 지도에서 www.acmicpc.net import sys from collections import deque input = sys.stdin.readline dx = [-1,1,0,0] dy = [0,0,-1,1] # L : 육지, W : 바다 max_value = float("-inf") n, m = map(int,input().split()) area = [list(input().rstrip()) for _ in range(n)] def bfs(x,y): global max_value visite..

Algorithm/BFS 2023.10.20

[백준 14889번] 스타트와 링크

14889번: 스타트와 링크 예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다. www.acmicpc.net n = int(input()) visited = [False for _ in range(n)] data = [list(map(int, input().split())) for _ in range(n)] min_value = float("inf") def dfs(depth,index): global min_value if depth == n//2: power1, power2 = 0, 0 for i in range(n): for j in range(n): if visited[i] and v..

[백준 2798번] 블랙잭

2798번: 블랙잭 첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장 www.acmicpc.net n, m = map(int,input().split()) card_list = list(map(int,input().split())) visited = [False]*n min_diff = float("inf") def dfs(total,count): global min_diff global answer diff = m - total if count == 3: if diff >= 0 and diff < min_diff: min_d..

[백준 7562번] 나이트의 이동

7562번: 나이트의 이동 체스판 위에 한 나이트가 놓여져 있다. 나이트가 한 번에 이동할 수 있는 칸은 아래 그림에 나와있다. 나이트가 이동하려고 하는 칸이 주어진다. 나이트는 몇 번 움직이면 이 칸으로 이동할 수 www.acmicpc.net from collections import deque t = int(input()) dx = [-1,-2,-2,-1,1,2,2,1] dy = [-2,-1,1,2,-2,-1,1,2] for _ in range(t): n = int(input()) start_x, start_y = map(int,input().split()) dest_x, dest_y = map(int,input().split()) visited = [[-1]*n for _ in range(n)] d..

Algorithm/BFS 2023.10.18

[백준 1406번] 에디터

1406번: 에디터 첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수 www.acmicpc.net import sys input = sys.stdin.readline string = list(input().rstrip()) m = int(input().rstrip()) command_list = [tuple(input().rstrip().split()) for _ in range(m)] stack = [] for command in command_list: if len(command) == 1: # L / D / B인 경우 if command[0] == 'L'..