본문 바로가기

Algorithm(CodeTree, Python)127

[코드트리 물체 단위로 완전탐색] 선분 3개 지우기 python # n 입력 n = int(input()) # line_pos line_pos = list() # line_pos 입력 for _ in range(n): line_pos.append(tuple(map(int, input().split()))) # 함수들 # is_duplicated(a, b, c) def is_duplicated(a, b, c): # temp -> 선분들의 영역을 표시해 줄 리스트 temp = [0] * 101 # 영역 표시 시작 for i in range(n): # 영역 표시하지 않을 선분일 경우 if i == a or i == b or i == c: # skip continue # 영역 표시할 선분일 경우 # 언팩킹 s, e = line_pos[i] # 영역 표시 for j in ra.. 2022. 12. 19.
[코드트리 물체 단위로 완전탐색] 스승의 은혜 2 python 여러 풀이가 있을 것 같은데, 완전탐색 답게 풀어보았다. 매 케이스마다 temp라는 배열을 만들 때, temp = price_list 이런식으로 하면 큰일날 수 있다. 배열을 복사할 때는 반드시 temp = original_list[:]을 사용해야함을 유의하자. # n, b 입력 n, b = map(int, input().split()) # price_list price_list = list() # price_list 입력 for _ in range(n): price_list.append(int(input())) # 함수들 # simulate(idx) def simulate(idx): # temp -> price_list 복사 temp = price_list[:] # 현재 idx의 가격을 반값으로 바꿔주.. 2022. 12. 18.
[코드트리 물체 단위로 완전탐색] 겹치지 않는 선분 2 python # n 입력 n = int(input()) # line_list line_list = list() # line_list 입력 for _ in range(n): line_list.append(tuple(map(int, input().split()))) # 함수들 # is_duplicated(k) def is_duplicated(k): # 현재 선분의 x1, x2 언팩킹 x1, x2 = line_list[k] # 비교할 선분들 탐색 시작 for i in range(n): # 현재 선분 if i == k: # skip continue # 이외의 선분(비교 선분) 언팩킹 other_x1, other_x2 = line_list[i] # 현재 선분보다 비교 선분이 뒤에서 출발하는 선분일 경우 if other_x1 .. 2022. 12. 17.
[코드트리 물체 단위로 완전탐색] 이상한 폭탄 2 python 의외로 엄청 고생한 문제였다. 중요한것은 범위 '내'에 같은 번호의 폭탄이 있는 경우를 찾는 것인데, 나는 범위가 '존재하는지'에 핀토가 꽃혀 계속 해결에 곤란을 겪었다. # n, k 입력 n, k = map(int, input().split()) # bombs bombs = list() # bombs 입력 for _ in range(n): bombs.append(int(input())) # 설계 # max_num_bomb max_num_bomb = -1 # 완전 탐색 시작 for i in range(n): for j in range(i+1, n): # 범위에 해당 되면서 같은 번호면 if j - i 2022. 12. 17.
[코드트리 물체 단위로 완전탐색] 개발자의 순위 python # k, n 입력 k, n = map(int, input().split()) # table table = list() # table 입력 for _ in range(k): table.append(list(map(int, input().split()))) # 함수들 # is_ok(a, b) def is_ok(a, b): # 모든 순위를 선회 for i in range(1, k): # 해당 순위 내에서 for j in range(n): # 앞 요소를 발견하면 if table[i][j] == a: # 순위를 기록 rank_a = j # 뒷 요소를 발견하면 elif table[i][j] == b: # 순위를 기록 rank_b = j # 한번이라도 순위가 맞지않으면 if rank_a > rank_b: # 실패 r.. 2022. 12. 16.
[코드트리 물체 단위로 완전탐색] 운행 되고 있는 시간 python # n 입력 n = int(input()) # work_time work_time = list() # work_time 입력 for _ in range(n): work_time.append(tuple(map(int, input().split()))) # 함수들 # calc(k) def calc(k): # curr_time_table curr_time_table = [0] * 1001 # 운행되고 있는 시간 체크 for i in range(n): # 제외하는 인덱스일 경우 if i == k: # skip continue # 이외의 경우 # 언팩킹 s, e = work_time[i] # 운행 시간 체크 for j in range(s, e): curr_time_table[j] = 1 # curr_total c.. 2022. 12. 16.
[코드트리 물체 단위로 완전탐색] 삼각형 만들기 python # n 입력 n = int(input()) # pos_list pos_list = list() # pos_list 입력 for _ in range(n): pos_list.append(tuple(map(int, input().split()))) # 함수들 # x_parallel(a, b, c): def x_parallel(a, b, c): # x1, y1 언팩킹 x1, y1 = pos_list[a] # x2, y2 언팩킹 x2, y2 = pos_list[b] # x3, y3 언팩킹 x3, y3 = pos_list[c] # 한 쌍이라도 y값이 같은 쌍이 있으면 통과 return y1 == y2 or y2 == y3 or y1 == y3 # y_parallel(a, b, c): def y_parallel(a,.. 2022. 12. 15.
[코드트리 물체 단위로 완전탐색] 가장 가까운 두 점 사이의 거리 python # n 입력 n = int(input()) # pos_list pos_list = list() # pos_list 입력 for _ in range(n): pos_list.append(tuple(map(int, input().split()))) # 함수들 # calc(a, b) def calc(a, b): # x_1, y_1, x_2, y_2 언팩킹 x_1, y_1 = pos_list[a] x_2, y_2 = pos_list[b] # curr_dist curr_dist = (x_1 - x_2) ** 2 + (y_1 - y_2) ** 2 # 반환 return curr_dist # 설계 # min_dist import sys min_dist = sys.maxsize # 완전 탐색 시작 for i in rang.. 2022. 12. 15.
[코드트리 물체 단위로 완전탐색] 좌표평면 위의 특정 구역 2 python # n 입력 n = int(input()) # pos_list pos_list = list() # pos_list 입력 for _ in range(n): pos_list.append(tuple(map(int, input().split()))) # 함수들 # calc(x, y) def calc(x, y): # max_x, min_x, max_y, min_y max_x, min_x = -sys.maxsize, sys.maxsize max_y, min_y = -sys.maxsize, sys.maxsize # 해당 좌표를 제외한 좌표를 탐색 for curr_x, curr_y in pos_list: # 해당 좌표라면 if curr_x == x and curr_y == y: # 스킵 continue # 아니라면 e.. 2022. 12. 15.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 개발팀의 능력 python # ability_list 입력 ability_list = list(map(int, input().split())) # 함수들 # all_diff(a, b, c) def all_diff(a, b, c): # team_1, team_2 team_1, team_2 = ability_list[a] + ability_list[b], ability_list[c] # team_3 team_3 = sum(ability_list) - team_1 - team_2 # 서로 다른지 반환 return team_1 != team_2 and team_2 != team_3 and team_1 != team_3 # calc(a, b, c) def calc(a, b, c): # team_1, team_2 team_1, team_2 .. 2022. 12. 14.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 개발자의 능력 2 python 파이썬 사용자는 참 축복받은 ㅋㅋㅋㅋ max, min 이런거 만들어주셔서 감사합니다. # ability_list 입력 ability_list = list(map(int, input().split())) # 함수들 # calc(a, b, c, d) def calc(a, b, c, d): # sum_1 sum_1 = ability_list[a] + ability_list[b] # sum_2 sum_2 = ability_list[c] + ability_list[d] # sum_3 sum_3 = sum(ability_list) - sum_1 - sum_2 # 반환 return max(sum_1, sum_2, sum_3) - min(sum_1, sum_2, sum_3) # 설계 # min_diff import s.. 2022. 12. 13.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 숫자 카운트 python 숫자야구를 파이썬으로 만들어볼줄이야,, 처음엔 당황했지만, 조건 하나씩 필터링해가며 비교해보면 될 듯 해서 만들어봤더니 돌아갔다. # n 입력 n = int(input()) # conditions conditions = [] # conditions 입력 for _ in range(n): num, cnt_1, cnt_2 = input().split() # 형 변환 cnt_1, cnt_2 = int(cnt_1), int(cnt_2) conditions.append([num, cnt_1, cnt_2]) # 함수들 # cnt_2_is_ok(k, correct_cnt, a, b, c) def cnt_2_is_ok(k, correct_cnt, a, b, c): # curr_cnt curr_cnt = 0 # k 분해.. 2022. 12. 12.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 두 가지로 열리는 자물쇠 python 원형에서 조건 따질때, 절댓값으로 해결하면 편하다는것을 기억하도록 하자는 교훈,,, # n 입력 n = int(input()) # comb_1 입력 comb_1 = list(map(int, input().split())) # comb_2 입력 comb_2 = list(map(int, input().split())) # 함수들 # open_with_comb_1(a, b, c) def open_with_comb_1(a, b, c): # a, b, c와 comb_1[0], comb_1[1], comb_1[2] 각각의 간격이 모두 2 이내이면 통과 return (abs(comb_1[0] - a) = n-2) and \ (abs(comb_1[1] - b) = n-2) and \ (abs(comb_1[2] - c).. 2022. 12. 12.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 개발자의 능력 3 python # ability_list ability_list = list(map(int, input().split())) # 함수들 # calc(a, b, c) def calc(a, b, c): # a, b, c 번째의 합 sum_1 = ability_list[a] + ability_list[b] + ability_list[c] # 이외의 합 sum_2 = sum(ability_list) - sum_1 # 두 값의 차를 반환 return abs(sum_1 - sum_2) # 설계 # min_diff import sys min_diff = sys.maxsize # 완전 탐색 시작 for i in range(4): for j in range(i+1, 5): for k in range(j+1, 6): # min_diff.. 2022. 12. 11.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 한 가지로 열리는 자물쇠python # n 입력 n = int(input()) # password 입력 password = list(map(int, input().split())) # 함수들 # is_open(a, b, c) def is_open(a, b, c): # 하나라도 차가 2 이하면 열림 return abs(a - password[0]) 2022. 12. 11.
[코드트리 구간 단위로 완전탐색] 밭의 높이를 고르게하기 python # n, h, t 입력 n, h, t = map(int, input().split()) # field_list 입력 field_list = list(map(int, input().split())) # 함수들 # calc(s, e) def calc(s, e): # curr_cost curr_cost = 0 # 해당 범위 내에서 for i in range(s, e): # curr_cost에 차이 추가 curr_cost += abs(field_list[i] - h) # 반환 return curr_cost # 설계 # lowest_cost import sys lowest_cost = sys.maxsize # 완전 탐색 시작 for i in range(n-t+1): # lowest_cost 업데이트 lowest_.. 2022. 12. 10.
[코드트리 구간 단위로 완전탐색] G or H 2 python # n 입력 n = int(input()) # linear linear = [0] * 101 # pos, alpha 입력 for _ in range(n): pos, alpha = input().split() # linear에 입력 linear[int(pos)] = alpha # 함수들 # exists(s, e) def exists(s, e): # 양 끝에 사람이 있는지 반환 return linear[s] and linear[e-1] # only_g(s, e) def only_g(s, e): # g_cnt g_cnt = 0 # 현 범위 중 for i in range(s, e): # 한 번이라도 'H'가 나오면 if linear[i] == 'H': # 실패 return False # 다 돌고나서 # g_cn.. 2022. 12. 9.
[코드트리 구간 단위로 완전탐색] 바구니 안의 사탕 2 python 문제에서 말하는게 너무 애매하다. 1. 바구니가 여러 개 들어갈 수 있다는 것이 동일선상의 여러 개 중 가장 큰 하나를 선택해야 한다는 것인지, 하나의 바구니로 합쳐진다는 것인지 알 수 없었다. 2. 앞 뒤 시작점이 모두 범위 내에 들어야 하는지 아닌지 명확하지 않다. # n, k 입력 n, k = map(int, input().split()) # linear linear = [0] * 401 # candy, pos 입력 for _ in range(n): candy, pos = map(int, input().split()) # linear에 추가 linear[pos] += candy # 함수들 # in_range(s, e) def in_range(s, e): # s가 양수이고, e가 400 이하면 통과 .. 2022. 12. 8.