본문 바로가기

Algorithm/BOJ133

[백준] 17471번 - 게리맨더링 (파이썬) # 게리멘더링 import sys from collections import deque MAX = sys.maxsize input = sys.stdin.readline N = int(input()) person = [0] + list(map(int, input().split())) arr = [[] for _ in range(N+1)] for i in range(1, N+1): temp = deque(map(int, input().split())) temp.popleft() arr[i] = list(temp) def bfs(area): q = deque() visited = [False] * (N+1) q.append(area[0]) visited[area[0]] = True temp = 0 count .. 2022. 2. 5.
[백준] 7453번 - 합이 0인 네 정수 (파이썬) # 합이 0인 네 정수 import sys input = sys.stdin.readline N = int(input()) A, B, C, D = [], [], [], [] for _ in range(N): a, b, c, d = map(int, input().split()) A.append(a) B.append(b) C.append(c) D.append(d) AandB = [] for a in A: for b in B: AandB.append(a+b) AandB.sort() CandD = [] for c in C: for d in D: CandD.append(c+d) CandD.sort() result = 0 left, right = 0, len(CandD)-1 sum = 0 while 0 0: righ.. 2022. 2. 5.
[백준] 1956번 - 운동 (파이썬) # 운동 import sys input = sys.stdin.readline MAX = sys.maxsize V, E = map(int, input().split()) arr = [[MAX for _ in range(V+1)] for _ in range(V+1)] for _ in range(E): a, b, cost = map(int, input().split()) arr[a][b] = cost for k in range(1, V+1): for i in range(1, V+1): for j in range(1, V+1): if arr[i][j] > arr[i][k]+arr[k][j]: arr[i][j] = arr[i][k]+arr[k][j] result = MAX for i in range(1, V+1).. 2022. 2. 5.
[백준] 1939번 - 중량제한 (파이썬) # 중량제한 import sys from collections import deque MAX = sys.maxsize input = sys.stdin.readline N, M = map(int, input().split()) arr = [[] for _ in range(N+1)] weight = [MAX] * (N+1) for _ in range(M): a, b, cost = map(int, input().split()) arr[a].append((b, cost)) arr[b].append((a, cost)) start_city, arrive_city = map(int, input().split()) def bfs(mid): q = deque() q.append(start_city) visited[sta.. 2022. 2. 4.
[백준] 2661번 - 좋은수열 (파이썬) # 좋은수열 def choose_num(result, count): for i in range(1, count//2+1): if str(result)[count-i:] == str(result)[count-2*i:count-i]: return if count == N: print(result) exit(0) for i in range(1, 4): temp = result * 10 + i choose_num(temp, count+1) N = int(input()) choose_num(0, 0) 백트랙킹 1. 1번부터 3번까지의 숫자를 총 N개를 골라서 result끝자리에 계속 더해준다 2. 만약 도중에 나쁜 수열에 걸리면 return 3. 좋은 수열이면서 제일 먼저 count가 N인 수열은 제일 작은 수이.. 2022. 2. 4.
[백준] 2623번 - 음악프로그램 (파이썬) # 음악프로그램 import sys input = sys.stdin.readline N, M = map(int, input().split()) front = [0] * (N+1) arr = [[] for _ in range(N+1)] for i in range(1, M+1): temp = list(map(int, input().split())) for j in range(1, temp[0]): arr[temp[j]].append(temp[j+1]) front[temp[j+1]] += 1 result = [] for _ in range(N): for i in range(1, N+1): if front[i] == 0 and i not in result: result.append(i) for j in rang.. 2022. 2. 4.
[백준] 2458번 - 키 순서 (파이썬) # 키 순서 import sys input = sys.stdin.readline N, M = map(int, input().split()) arr = [[0]*(N+1) for _ in range(N+1)] for _ in range(M): a, b = map(int, input().split()) arr[a][b] = 1 for k in range(1, N+1): for i in range(1, N+1): for j in range(1, N+1): if arr[i][j] == 0: if arr[i][k] == 1 and arr[k][j] == 1: arr[i][j] = 1 result = 0 for i in range(1, N+1): sum = 0 for j in range(1, N+1): sum +=.. 2022. 2. 2.