diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 7825c25..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/yeonwoo/1051.py b/yeonwoo/1051.py new file mode 100644 index 0000000..5123bbc --- /dev/null +++ b/yeonwoo/1051.py @@ -0,0 +1,26 @@ +# 1051 +# 숫자 정사각형 + +def find_square(n, m): + number = rectangle[n][m] # 기준이 될 숫자 + edge = 1 # 변의 길이 + max_edge = 0 # 정사각형의 변 길이 최대값 + while n+edge< N and m+edge < M: # 직사각형 안에서 진행 + if number == rectangle[n+edge][m] == rectangle[n][m+edge] == rectangle[n+edge][m+edge]: # 꼭짓점에 쓰여 있는 수가 모두 같은 정사각형을 찾는다면 + max_edge = edge + edge += 1 + return max_edge + +N, M = map(int, input().split(' ')) +rectangle = [] +for _ in range(N): + rectangle.append(list(input())) +# print(rectangle) + +result_edge = 0 +for n in range(N): + for m in range(M): + # 모든 위치에서 시작하여 정사각형을 찾음 + max_edge = find_square(n, m) + if max_edge > result_edge: result_edge = max_edge +print((result_edge+1)**2) \ No newline at end of file diff --git a/yeonwoo/11725.py b/yeonwoo/11725.py new file mode 100644 index 0000000..16da94d --- /dev/null +++ b/yeonwoo/11725.py @@ -0,0 +1,31 @@ +# 11725 +# 트리의 부모 찾기 + +from collections import deque + +def find_parent(graph, start): + parent = [0 for _ in range(len(graph))] # 각 노드의 부모 노드 + visited = [] # 탐색한 노드를 체크하기 위해 + queue = deque([start]) + while queue: + node = queue.popleft() + visited.append(node) + for n in graph[node]: + if not parent[n]: # 아직 탐색하지 않은 노드라면(부모 노드가 설정되지 않은 노드라면) + queue.append(n) + parent[n] = node # 해당 노드의 부모 노드를 설정 + + # print(node, end=' ') + # print(parent, end=' ') + # print(queue) + return parent + +N = int(input()) +graph = [ [] for _ in range(N+1) ] # 각 노드와 연결된 노드들 +for _ in range(N-1): + n1, n2 = map(int, input().split(' ')) + graph[n1].append(n2) + graph[n2].append(n1) + +parent = find_parent(graph, 1) +for i in range(2, N+1): print(parent[i]) \ No newline at end of file diff --git a/yeonwoo/1260.py b/yeonwoo/1260.py new file mode 100644 index 0000000..503770e --- /dev/null +++ b/yeonwoo/1260.py @@ -0,0 +1,34 @@ +# 1260 +# DFS와 BFS + +from collections import deque + +DFS_visited = [] # DFS를 수행한 결과 +def dfs(graph, start): + if start in DFS_visited: return # 이미 방문한 노드 + + DFS_visited.append(start) + for node in graph[start]: dfs(graph, node) # 해당 노드와 연결된 노드로 DFS를 계속함 + +BFS_visited = [] # BFS를 수행한 결과 +def bfs(graph, start): + queue = deque([start]) + while queue: + node = queue.popleft() + if node not in BFS_visited: # 아직 방문한 적 없는 노드만 탐색 + BFS_visited.append(node) + for n in graph[node]: queue.append(n) + + +N, M, V = map(int, input().split(' ')) +graph = [ [] for _ in range(N+1) ] # 각 정점과 연결된 정점들 +for _ in range(M): + n1, n2 = map(int, input().split(' ')) + graph[n1].append(n2) + graph[n2].append(n1) +for n in range(1, N+1): graph[n].sort() + +dfs(graph, V) +bfs(graph, V) +print(*(DFS_visited)) +print(*(BFS_visited)) \ No newline at end of file diff --git a/yeonwoo/1283.py b/yeonwoo/1283.py new file mode 100644 index 0000000..803f1f9 --- /dev/null +++ b/yeonwoo/1283.py @@ -0,0 +1,41 @@ +# 1283 +# 단축키 지정 + +N = int(input()) + +shortcut = [] # 각 옵션의 단축키와, 해당 위치 +def assign_shortcut(option): + shortcut_key = list(map(lambda x: x[0], shortcut)) # 단축키들 + # print(shortcut_key) + option = option.upper() # 대문자로 변환 + + for i in range(len(option)): + if i == 0 or option[i-1] == ' ': # 단어의 첫 글자 + if option[i] not in shortcut_key: # 단어의 첫 글자가 단축키로 지정되어 있지 않다면 + shortcut.append([option[i], i]) # 단축키와 해당 위치를 리스트에 저장 + return + + # 모든 단어의 첫 글자가 이미 단축키로 지정되어 있다면 + for i in range(len(option)): + if option[i] != ' ' and option[i] not in shortcut_key: # 왼쪽에서부터 차례대로 알파벳을 보면서 단축키로 지정 안 된 것이 있다면 + shortcut.append([option[i], i]) # 단축키로 지정 + return + + # 어떠한 것도 단축키로 지정할 수 없다면 그냥 놔둠 + shortcut.append(['', -1]) + +options = [] +for _ in range(N): + option = input() + assign_shortcut(option) + options.append(option) +# print(shortcut) + +for n in range(N): # 각 옵션에 단축키를 표시해서 출력 + index = shortcut[n][1] # 단축키가 있는 위치 + + for i in range(len(options[n])): # 출력 + if i == index: # 단축키가 있는 알파벳을 출력할 때 + print('['+options[n][i]+']', end='') + else: print(options[n][i], end='') + print() \ No newline at end of file diff --git a/yeonwoo/1325.py b/yeonwoo/1325.py new file mode 100644 index 0000000..953ba14 --- /dev/null +++ b/yeonwoo/1325.py @@ -0,0 +1,42 @@ +# 1325 +# 효율적인 해킹 + +from collections import deque + +N, M = map(int, input().split(' ')) # N개의 컴퓨터, M개 줄 입력 +trust = [ [] for _ in range(N+1) ] # 각 컴퓨터를 신뢰하는 컴퓨터들. 해당 컴퓨터를 해킹하면 함께 해킹할 수 있는 컴퓨터 +for _ in range(M): + A, B = map(int, input().split(' ')) # A가 B를 신뢰 + trust[B].append(A) # B를 해킹하면 A도 해킹할 수 있음 +# for n in range(1, len(trust)): +# print(n, end=" ") +# print(trust[n]) + +outputs = [] +max_hacking = 0 +def bfs(trust, start): + global max_hacking + result = 0 # 해당 컴퓨터를 해킹하면 한 번에 해킹할 수 있는 모든 컴퓨터의 개수 + visited = [False] * (N+1) + queue = deque([start]) + visited[start] = True + while queue: + # print(hacking[start]) + # print(queue) + computer = queue.popleft() + for com in trust[computer]: + if not visited[com]: + visited[com] = True + result += 1 + queue.append(com) + if result > max_hacking: + outputs.clear() + outputs.append(start) + max_hacking = result + elif result == max_hacking: outputs.append(start) + +for computer in range(1, N+1): bfs(trust, computer) +# print(hacking) + +# 한 번에 가장 많은 컴퓨터를 해킹할 수 있는 컴퓨터를 찾음 +print(*(outputs)) \ No newline at end of file diff --git a/yeonwoo/1449.py b/yeonwoo/1449.py new file mode 100644 index 0000000..dd86eca --- /dev/null +++ b/yeonwoo/1449.py @@ -0,0 +1,19 @@ +# 1449 +# 수리공 항승 + +N, L = map(int, input().split(' ')) # 물이 새는 곳의 개수 N, 테이프의 길이 L +holes = list(map(int, input().split(' '))) +holes.sort() # 오름차순으로 정렬 + +output = 1 # 필요한 테이프의 수 +tape = [holes[0]-0.5, holes[0]-0.5+L] # 가장 최근에 붙인 테이프가 붙여져 있는 위치 +for i in range(1, N): + # 해당 물이 새는 곳을 이 테이프로 막을 수 있는지 확인 + if holes[i]-0.5 >= tape[0] and holes[i]+0.5 <= tape[1]: # 막을 수 있다면 + continue # 패스 + else: # 막을 수 없다면 + output += 1 # 필요한 테이프를 하나 더 추가 + tape[0] = holes[i]-0.5 + tape[1] = holes[i]-0.5+L + +print(output) \ No newline at end of file diff --git a/yeonwoo/14500.py b/yeonwoo/14500.py new file mode 100644 index 0000000..1caf2c8 --- /dev/null +++ b/yeonwoo/14500.py @@ -0,0 +1,82 @@ +# 14500 +# 테트로미노 + +N, M = map(int, input().split(' ')) +paper = [] +for _ in range(N): paper.append(list(map(int, input().split(' ')))) +# print(paper) + +max_num = 0 +for n in range(N): + for m in range(M): + + # 1번 테트로미노 + if m+3 < M: + num = paper[n][m] + paper[n][m+1] + paper[n][m+2] + paper[n][m+3] + if num > max_num: max_num = num + if n+3 < N: + num = paper[n][m] + paper[n+1][m] + paper[n+2][m] + paper[n+3][m] + if num > max_num: max_num = num + + # 2번 테트로미노 + if n+1 < N and m+1 < M: + num = paper[n][m] + paper[n][m+1] + paper[n+1][m] + paper[n+1][m+1] + if num > max_num: max_num = num + + # 3번 테트로미노 + if n+2 < N: + if m+1 < M: + num = paper[n][m] + paper[n+1][m] + paper[n+2][m] + paper[n][m+1] + if num > max_num: max_num = num + + num = paper[n][m] + paper[n+1][m] + paper[n+2][m] + paper[n+2][m+1] + if num > max_num: max_num = num + if m-1 > -1: + num = paper[n][m] + paper[n+1][m] + paper[n+2][m] + paper[n][m-1] + if num > max_num: max_num = num + + num = paper[n][m] + paper[n+1][m] + paper[n+2][m] + paper[n+2][m-1] + if num > max_num: max_num = num + if m+2 < M: + if n+1 < N: + num = paper[n][m] + paper[n][m+1] + paper[n][m+2] + paper[n+1][m] + if num > max_num: max_num = num + + num = paper[n][m] + paper[n][m+1] + paper[n][m+2] + paper[n+1][m+2] + if num > max_num: max_num = num + if n-1 > -1: + num = paper[n][m] + paper[n][m+1] + paper[n][m+2] + paper[n-1][m] + if num > max_num: max_num = num + + num = paper[n][m] + paper[n][m+1] + paper[n][m+2] + paper[n-1][m+2] + if num > max_num: max_num = num + + # 4번 테트로미노 + if n+2 < N and m+1 < M: + num = paper[n][m] + paper[n+1][m] + paper[n+1][m+1] + paper[n+2][m+1] + if num > max_num: max_num = num + + num = paper[n][m+1] + paper[n+1][m] + paper[n+1][m+1] + paper[n+2][m] + if num > max_num: max_num = num + if n+1 < N and m+2 < M: + num = paper[n][m+1] + paper[n][m+2] + paper[n+1][m] + paper[n+1][m+1] + if num > max_num: max_num = num + + num = paper[n][m] + paper[n][m+1] + paper[n+1][m+1] + paper[n+1][m+2] + if num > max_num: max_num = num + + # 5번 테트로미노 + if n+1 < N and m+2 < M: + num = paper[n][m] + paper[n][m+1] + paper[n][m+2] + paper[n+1][m+1] + if num > max_num: max_num = num + + num = paper[n+1][m] + paper[n+1][m+1] + paper[n+1][m+2] + paper[n][m+1] + if num > max_num: max_num = num + if n+2 < N and m+1 < M: + num = paper[n][m] + paper[n+1][m] + paper[n+2][m] + paper[n+1][m+1] + if num > max_num: max_num = num + + num = paper[n][m+1] + paper[n+1][m+1] + paper[n+2][m+1] + paper[n+1][m] + if num > max_num: max_num = num + +print(max_num) \ No newline at end of file diff --git a/yeonwoo/17144.py b/yeonwoo/17144.py new file mode 100644 index 0000000..a48debb --- /dev/null +++ b/yeonwoo/17144.py @@ -0,0 +1,66 @@ +# 17144 +# 미세먼지 안녕! + +from copy import deepcopy + +R, C, T = map(int, input().split(' ')) # 격자판 RxC, T초 후의 상태 +next_room = [] # 다음에 변화될 상태 +for _ in range(R): next_room.append(list(map(int, input().split(' ')))) +dir = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 동서남북 방향 + +room = [] +air_purifier = [] # 공기청정기 위치 +for _ in range(T): # T초 동안 진행됨 + room = deepcopy(next_room) + # 미세먼지 확산 + for r in range(R): + for c in range(C): + if room[r][c] > 0: # 해당 칸에 미세먼지가 있다면 + # 각 방향에 위치한 칸이 벽이 아니고, 공기청정기가 없다면 미세먼지 확산 + if r > 0 and room[r-1][c] > -1: + next_room[r-1][c] += room[r][c] // 5 + next_room[r][c] -= room[r][c] // 5 + if c > 0 and room[r][c-1] > -1: + next_room[r][c-1] += room[r][c] // 5 + next_room[r][c] -= room[r][c] // 5 + if r < R-1 and room[r+1][c] > -1: + next_room[r+1][c] += room[r][c] // 5 + next_room[r][c] -= room[r][c] // 5 + if c < C-1 and room[r][c+1] > -1: + next_room[r][c+1] += room[r][c] // 5 + next_room[r][c] -= room[r][c] // 5 + elif room[r][c] == -1: air_purifier.append([r, c]) + + # 공기청정기 작동, 바람 순환 + # 반시계 방향 순환 + air_move = [] + r, c = air_purifier[0][0], air_purifier[0][1] + for i in range(c+1, C-1): air_move.append([r, i]) + for i in range(r, 0, -1): air_move.append([i, C-1]) + for i in range(C-1, c, -1): air_move.append([0, i]) + for i in range(0, r): air_move.append([i, c]) + # print(air_move) + for i in range(len(air_move)-1, 0, -1): + next_room[air_move[i][0]][air_move[i][1]] = next_room[air_move[i-1][0]][air_move[i-1][1]] + next_room[air_move[0][0]][air_move[0][1]] = 0 + + # 시계 방향 순환 + air_move = [] + r, c = air_purifier[1][0], air_purifier[1][1] + for i in range(c+1, C-1): air_move.append([r, i]) + for i in range(r, R-1): air_move.append([i, C-1]) + for i in range(C-1, c, -1): air_move.append([R-1, i]) + for i in range(R-1, r, -1): air_move.append([i, c]) + # print(air_move) + for i in range(len(air_move)-1, 0, -1): + next_room[air_move[i][0]][air_move[i][1]] = next_room[air_move[i-1][0]][air_move[i-1][1]] + next_room[air_move[0][0]][air_move[0][1]] = 0 + + # for r in range(R): print(next_room[r]) + +result = 0 +for r in range(R): + for c in range(C): + if next_room[r][c] > 0: # 미세먼지가 있다면 + result += next_room[r][c] # 해당 미세먼지 값을 합산 +print(result) \ No newline at end of file diff --git a/yeonwoo/17276.py b/yeonwoo/17276.py new file mode 100644 index 0000000..f02a70b --- /dev/null +++ b/yeonwoo/17276.py @@ -0,0 +1,31 @@ +# 17276 +# 배열 돌리기 + +from copy import deepcopy + +def rotate_45(array, n): # 배열을 시계 방향으로 45도 돌리는 함수 + result_array = deepcopy(array) + for i in range(n): + result_array[i][(n+1)//2-1] = array[i][i] # 주 대각선을 가운데 열로 옮김 + result_array[i][n-1-i] = array[i][(n+1)//2-1] # 가운데 열을 부 대각선으로 옮김 + result_array[(n+1)//2-1][n-1-i] = array[i][n-1-i] # 부 대각선을 가운데 행으로 옮김 + result_array[i][i] = array[(n+1)//2-1][i] # 가운데 행을 주 대각선으로 옮김 + return result_array + +N = int(input()) # 테스트 케이스의 개수 +result = [] # 결과값 리스트 +for _ in range(N): + n, d = map(int, input().split(' ')) + array = [] + for _ in range(n): + array.append(list(map(int, input().split(' ')))) + + if d < 0: d += 360 # 함수를 사용하기 위해 360도를 더해줌 + number = d // 45 # 45도로 몇 번 돌려야 하는지 + for _ in range(number): + result_array = rotate_45(array, n) + array = deepcopy(result_array) + + for i in range(n): result.append(array[i]) + +for i in range(len(result)): print(*(result[i])) \ No newline at end of file diff --git a/yeonwoo/17413.py b/yeonwoo/17413.py new file mode 100644 index 0000000..22b0867 --- /dev/null +++ b/yeonwoo/17413.py @@ -0,0 +1,32 @@ +# 17413 +# 단어 뒤집기 + +string = input() + +stack = [] # '<', '>', 문자를 담을 스택 +result = [] # 결과 문자열 +for element in string: + if element == '<': + while stack: result.append(stack.pop()) # 단어를 뒤집어서 저장 + stack.append('<') + result.append('<') + elif element == '>': + stack.pop() # 여기까지 태그 + result.append('>') + elif element == ' ': + if stack and stack[0] == '<': # 태그라면 + result.append(' ') # 뒤집기 없이 저장 + else: # 단어라면 + while stack: result.append(stack.pop()) # 단어를 뒤집어서 저장 + result.append(' ') + else: # 문자 + if stack and stack[0] == '<': # 태그라면 + result.append(element) # 뒤집지 않고 저장 + else: # 단어라면 + stack.append(element) +if stack and stack[0] == '<': + result.append('>') +else: + while stack: result.append(stack.pop()) # 단어를 뒤집어서 저장 + +print(''.join(result)) \ No newline at end of file diff --git a/yeonwoo/18114.py b/yeonwoo/18114.py new file mode 100644 index 0000000..05a2fef --- /dev/null +++ b/yeonwoo/18114.py @@ -0,0 +1,30 @@ +# 18114 +# 블랙 프라이데이 + +N, C = map(int, input().split()) # 물건의 개수 N, 제시하는 무게 C + +weight = list(map(int, input().split())) +weight.sort() # 오름차순으로 정렬 + +def find_combination(N, C): + max_index = N - 1 # 제시하는 무게보다 가벼운 최대 무게 + while max_index > 0 and weight[max_index] > C: max_index -= 1 + + # 물건 하나로 조합을 만족하는 경우 + if weight[max_index] == C: return 1 + + # 물건 여러 개로 조합을 만족하는 경우 + left = 0 + right = max_index + while left < right: + diff = C - (weight[left] + weight[right]) # 물건 두 개 무게의 합이 제시된 무게보다 얼마나 부족한지 + if weight[left] + weight[right] == C: return 1 # 두 물건 무게의 합이 C이면 조합을 찾은 것이므로 종료 + elif weight[left] + weight[right] > C: # 두 물건 무게의 합이 제시된 무게보다 크다면 + right -= 1 # 무게의 합을 줄여야 하므로 오른쪽 포인터를 한 칸 왼쪽으로 이동 + else: # 두 물건 무게의 합이 제시된 무게보다 작다면 + if diff in weight[left+1:right]: return 1 # 제시된 무게에서 부족한 값이 두 포인터가 가리키는 물건 사이에 있다면 종료 + left += 1 # 무게의 합을 키워야 하므로 왼쪽 포인터를 한 칸 오른쪽으로 이동 + + return 0 # 조합을 찾지 못함 + +print(find_combination(N, C)) \ No newline at end of file diff --git a/yeonwoo/1931.py b/yeonwoo/1931.py new file mode 100644 index 0000000..8f89622 --- /dev/null +++ b/yeonwoo/1931.py @@ -0,0 +1,21 @@ +# 1931 +# 회의실 배정 + +N = int(input()) + +meetings = [] +for n in range(N): + start, end = map(int, input().split(' ')) + meetings.append([start, end]) + +meetings.sort(key=lambda x:(x[1], x[0])) # 끝나는 시간을 기준으로 정렬 후 시작하는 시간을 기준으로 정렬 +# print(meetings) + +last_time = 0 # 현재 회의가 끝나는 시간 +result = 0 +for meeting in meetings: + if last_time <= meeting[0]: # 해당 회의를 시작할 수 있다면 + result += 1 # 회의 시작 + last_time = meeting[1] # 회의 끝나는 시간 업데이트 + +print(result) \ No newline at end of file diff --git a/yeonwoo/20055.py b/yeonwoo/20055.py new file mode 100644 index 0000000..03759d9 --- /dev/null +++ b/yeonwoo/20055.py @@ -0,0 +1,34 @@ +# 20055 +# 컨베이어 벨트 위의 로봇 + +from collections import deque + +N, K = map(int, input().split()) +power = deque(map(int, input().split())) # 해당 칸의 내구도 +belt = deque(0 for _ in range(N)) +num = 0 + +while power.count(0) < K: # 내구도가 0인 칸이 K개 이상이라면 종료 + num += 1 # 해당 순서가 몇 번째인지 + + # 벨트가 각 칸 위에 있는 로봇과 함께 회전 + power.rotate(1) + belt.rotate(1) + belt[0] = 0 + + belt[N-1] = 0 # 내리는 위치에 있는 로봇을 내림 + for n in range(N-2, -1, -1): # 벨트에 가장 먼저 올라간 로봇부터 각 칸에 있는 로봇이 이동할 수 있는지 체크 + if belt[n] == 1 and belt[n+1] == 0 and power[n+1] > 0: # 이동하려는 칸에 로봇이 없고, 그 칸의 내구도가 1 이상 남아있어야 이동 가능 + # 로봇이 이동 + belt[n] = 0 + belt[n+1] = 1 + power[n+1] -= 1 + + if power[0] > 0: # 올리는 위치에 있는 칸의 내구도가 0이 아니라면 + belt[0] = 1 # 올리는 칸에 로봇을 올림 + power[0] -= 1 + + # print(power, end=" ") + # print(belt) + +print(num) \ No newline at end of file diff --git a/yeonwoo/20164.py b/yeonwoo/20164.py new file mode 100644 index 0000000..f6bb2c9 --- /dev/null +++ b/yeonwoo/20164.py @@ -0,0 +1,37 @@ +# 20164 +# 홀수 홀릭 호석 + +from itertools import combinations + +result = [20, 0] # 결과 최솟값, 최댓값 +def make_odd(number, res): # 로직을 실행 + + if len(number) == 1: # 수가 한 자리 + if int(number) % 2 == 1: # 홀수라면 + res += 1 + + if res < result[0]: result[0] = res # 최솟값인지 체크 + if res > result[1]: result[1] = res # 최댓값인지 체크 + + elif len(number) == 2: # 수가 두 자리 + for num in number: # 홀수의 개수를 셈 + if int(num) % 2 == 1: # 홀수라면 + res += 1 + + new_number = str(int(number[0]) + int(number[1])) # 2개로 나눠서 합을 구하여 새로운 수로 생각 + make_odd(new_number, res) + + else: # 수가 세 자리 이상 + for num in number: # 홀수의 개수를 셈 + if int(num) % 2 == 1: # 홀수라면 + res += 1 + + section = list(combinations([i for i in range(1, len(number))], 2)) # 수를 분할시키는 위치의 경우의 수 + # print(section) + for s1, s2 in section: + new_number = str(int(number[:s1]) + int(number[s1:s2]) + int(number[s2:])) # 3개로 나눠서 합을 구하여 새로운 수로 생각 + make_odd(new_number, res) + +N = str(input()) # 문자열로 입력을 받음 +make_odd(N, 0) +print(*(result)) \ No newline at end of file diff --git a/yeonwoo/22251.py b/yeonwoo/22251.py new file mode 100644 index 0000000..e8282da --- /dev/null +++ b/yeonwoo/22251.py @@ -0,0 +1,32 @@ +# 22251 +# 빌런 호석 + +# 바꿀 층수는 N 이하여야 함, 층 자리수 K +# LED 중 최소 1개 최대 P개 반전, 실제 층은 X층 +N, K, P, X = map(int, input().split(' ')) + +LED = [ # 각 숫자에서 불이 들어오는 칸의 위치 + set([0, 1, 2, 4, 5, 6]), + set([2, 5]), + set([0, 2, 3, 4, 6]), + set([0, 2, 3, 5, 6]), + set([1, 2, 3, 5]), + set([0, 1, 3, 5, 6]), + set([0, 1, 3, 4, 5, 6]), + set([0, 2, 5]), + set([0, 1, 2, 3, 4, 5, 6]), + set([0, 1, 2, 3, 5, 6]) +] + +output = 0 # LED를 올바르게 반전시킬 수 있는 경우의 수 +before = [0 for _ in range(K-len(str(X)))] # 실제 층 +before += map(int, list(str(X))) +for n in range(1, N+1): # 최대값까지 + after = [0 for _ in range(K-len(str(n)))] # 실제 층 + after += map(int, list(str(n))) + + num_reverse = 0 # 반전시킨 LED의 수 + for i in range(K): num_reverse += len(LED[before[i]] ^ LED[after[i]]) + if num_reverse <= P: output += 1 + +print(output-1) # 실제 층도 체크되었으므로 1을 빼줌 \ No newline at end of file diff --git a/yeonwoo/2470.py b/yeonwoo/2470.py new file mode 100644 index 0000000..60b37be --- /dev/null +++ b/yeonwoo/2470.py @@ -0,0 +1,25 @@ +# 2470 +# 두 용액 + +N = int(input()) +water = list(map(int, input().split(' '))) + +water.sort() # 오름차순으로 정렬 +left = 0 # 왼쪽 포인터 +right = N - 1 # 오른쪽 포인터 + +output = [water[left], water[right]] # 특성값이 0에 가장 가까운 용액을 만들어내는 두 용액의 특성값 +while left < right: + + if abs(water[left] + water[right]) < abs(output[0] + output[1]): # 현재 결과값보다 더 0에 가까운 두 용액의 특성값 합을 찾았다면 + output = [water[left], water[right]] # 결과값을 업데이트 + + if water[left] + water[right] < 0: # 합이 0보다 작으면 특성값의 합을 키워야 하므로 + left += 1 # 왼쪽 포인터를 오른쪽으로 한 칸 이동 + elif water[left] + water[right] > 0: # 합이 0보다 크면 특성값의 합을 줄여야 하므로 + right -= 1 # 오른쪽 포인터를 왼쪽으로 한 칸 이동 + else: # 합이 0이라면 이미 특성값이 0에 가장 가까운 용액을 찾은 것이므로 + output = [water[left], water[right]] # 결과값을 업데이트하고 종료 + break + +print(*(output)) \ No newline at end of file diff --git a/yeonwoo/2606.py b/yeonwoo/2606.py new file mode 100644 index 0000000..80976c4 --- /dev/null +++ b/yeonwoo/2606.py @@ -0,0 +1,25 @@ +# 2606 +# 바이러스 + +from collections import deque + +computer = set([1]) # 1번 컴퓨터를 통해 웜 바이러스에 걸리게 되는 컴퓨터의 수 + +num_computer = int(input()) +N = int(input()) +computer = [ [] for _ in range(num_computer) ] # 각 컴퓨터와 연결되어 있는 컴퓨터 +for _ in range(N): + com1, com2 = map(int, input().split(' ')) + computer[com1-1].append(com2-1) + computer[com2-1].append(com1-1) +# print(computer) + +visited = [] # 이미 체크한 컴퓨터 +queue = deque([0]) # 1번 컴퓨터를 통해 웜 바이러스에 걸리게 되는 컴퓨터 +while queue: + start = queue.popleft() + if start not in visited: # 체크 안 한 컴퓨터라면 + visited.append(start) + for com in computer[start]: queue.append(com) + +print(len(visited)-1) # 1번 컴퓨터를 통해 웜 바이러스에 걸리게 되는 컴퓨터의 수 \ No newline at end of file diff --git a/yeonwoo/3085.py b/yeonwoo/3085.py new file mode 100644 index 0000000..fcd4709 --- /dev/null +++ b/yeonwoo/3085.py @@ -0,0 +1,57 @@ +# 3085 +# 사탕 게임 + +from copy import deepcopy + +result = 0 +def check_candy(candy): + global result + + for x in range(N): + color = '' + + # 행 체크 + number = 0 + for y in range(N): + if candy[x][y] == color: # 연속된 사탕이라면 + number += 1 + else: # 연속된 사탕이 아니라면 + if result < number: result = number # 최대 개수 업데이트 + number = 1 # 처음부터 다시 셈 + color = candy[x][y] + if result < number: result = number # 최대 개수 업데이트 + + # 열 체크 + number = 0 + for y in range(N): + if candy[y][x] == color: # 연속된 사탕이라면 + number += 1 + else: # 연속된 사탕이 아니라면 + if result < number: result = number # 최대 개수 업데이트 + number = 1 # 처음부터 다시 셈 + color = candy[y][x] + if result < number: result = number # 최대 개수 업데이트 + +N = int(input()) +candy = [] +for _ in range(N): + candy.append(list(input())) +# print(candy) + +for x in range(N): + for y in range(N): + # 행을 바꿈 + if x < N-1: + ex_candy = deepcopy(candy) + ex_candy[x][y], ex_candy[x+1][y] = candy[x+1][y], candy[x][y] + # for i in range(len(ex_candy)): print(ex_candy[i]) + check_candy(ex_candy) + + # 열을 바꿈 + if y < N-1: + ex_candy = deepcopy(candy) + ex_candy[x][y], ex_candy[x][y+1] = candy[x][y+1], candy[x][y] + # for i in range(len(ex_candy)): print(ex_candy[i]) + check_candy(ex_candy) + +print(result) \ No newline at end of file diff --git a/yeonwoo/7682.py b/yeonwoo/7682.py new file mode 100644 index 0000000..05042ba --- /dev/null +++ b/yeonwoo/7682.py @@ -0,0 +1,49 @@ +# 7682 +# 틱택토 + +def check(test, piece): # 해당 말이 3칸을 잇는데 성공했는지를 반환 + # 가로 + if piece == test[0] == test[1] == test[2]: return True + if piece == test[3] == test[4] == test[5]: return True + if piece == test[6] == test[7] == test[8]: return True + # 세로 + if piece == test[0] == test[3] == test[6]: return True + if piece == test[1] == test[4] == test[7]: return True + if piece == test[2] == test[5] == test[8]: return True + # 대각선 + if piece == test[0] == test[4] == test[8]: return True + if piece == test[2] == test[4] == test[6]: return True + +outputs = [] +while True: + test = input() + if test == 'end': break + + # print(test[0:3]) + # print(test[3:6]) + # print(test[6:9]) + # print('-----------') + + num_X, num_O = 0, 0 + for t in test: # 각 말의 개수를 셈 + if t == 'X': num_X += 1 + elif t == 'O': num_O += 1 + + if num_X - num_O == 1: # X가 O보다 1개 많은 경우 -> X가 승리 or 무승부 + if check(test, 'X'): # X가 성공한 경우 + if check(test, 'O'): outputs.append('invalid') # O도 성공했으면 invalid + else: outputs.append('valid') # O가 실패했어야 가능 + else: # X가 실패한 경우 + if num_X + num_O < 9: outputs.append('invalid') # 게임판을 모두 채우지 못했으면 invalid + elif check(test, 'O'): outputs.append('invalid') # O가 성공했으면 invalid + else: outputs.append('valid') # O가 실패했어야 가능 + elif num_X == num_O: # X와 O의 개수가 같음 -> O가 승리 + if check(test, 'O'): # O가 성공한 경우 + if check(test, 'X'): outputs.append('invalid') # X도 성공했으면 invalid + else: outputs.append('valid') # X가 실패했어야 가능 + else: # O가 실패한 경우 + outputs.append('invalid') # 불가능 + else: # 나머지 -> 불가능 + outputs.append('invalid') + +for output in outputs: print(output) \ No newline at end of file