목록Algorithms/Simulation (18)
from collections import defaultdict # 에러 # 루트 노드가 2개인 예외가 있을 수 있음 lst = [] while True: temp = list(map(int, input().split())) if temp: if temp[-1] == -1 and temp[-2] == -1: break else: lst.extend(temp) temp = defaultdict(list) cases = [] for i in range(0, len(lst), 2): if lst[i] == 0 and lst[i+1] == 0: cases.append(temp) temp = defaultdict(list) else: temp[lst[i+1]].append(lst[i]) for i, case in..
# 1. 배열판 만들기 # 2. 움직인 칸 합 # 3. 합 출력 ## 메모리 초과 n, k = map(int, input().split()) jumps = list(input()) fields = [[0]*n for _ in range(n)] # U D L R dirs = {'U': (-1, 0), 'D': (1, 0), 'L': (0, -1), 'R': (0, 1)} answer = [1] def make_zigzag_list(fields, n): cnt = 1 fields[0][0] = cnt change_dir = 0 for i in range(1, n): a = list(range(i+1)) b = list(range(i, -1, -1)) ..
from collections import deque def move(cur1, cur2, board): dirs = [(1,0), (0,1), (-1,0), (0,-1)] ret = [] for y, x in dirs: if board[cur1[0]+y][cur1[1]+x] == 0 and board[cur2[0]+y][cur2[1]+x] == 0: ret.append({(cur1[0]+y, cur1[1]+x), (cur2[0]+y, cur2[1]+x)}) rotate = [1,-1] # 가로로 위치해있는 로봇 회전 if cur1[0] == cur2[0]: for r in rotate: if board[cur1[0]+r][cur1[1]] == 0 and board[cur2[0]+r][cur2[1]] =..
# return 최소한의 친구들 투입 # 외벽 길이(1~200), 취약 지점 위치(길이:1~15, 원소:0~n-1), 한시간동안 이동가능 거리(길이:1~8, 원소:1~100) # 1. 시작점 선정 # 2. 각 시작점 별 시계 반시계 방향으로 갔을 때 다 들리는 값 구하기 from collections import deque from itertools import permutations # 친구 투입 후 다음 친구 투입 def nxt_idx(queue, d, start_idx=0): # 시작점 start_num = queue[start_idx] # i = 거리 for i in range(1, d+1): try: # 이동 가능 거리 안에서 다음 취약점 포함되는 경우 if queue[start_idx + 1]..
def solution(lines): S, E = [], [] total_lines = 0 for line in lines: total_lines += 1 d, s, t = line.split(' ') t = float(t[:-1]) hh, mm, ss = s.split(':') seconds = float(hh) * 3600 + float(mm) * 60 + float(ss) E.append(seconds + 1) S.append(seconds - t + 0.001) S.sort() cur_traffic = 0 max_traffic = 0 cnt_E = 0 cnt_S = 0 while ((cnt_E < total_lines) and (cnt_S < total_lines)):..
def rotate(maps): return [list(reversed(i)) for i in zip(*maps)] def solution(key, lock): hole = 0 # hole 개수 총합 for y in range(len(lock)): for x in range(len(lock[0])): if lock[y][x] == 0: hole += 1 for _ in range(4): # 길이가 3 인경우 -2 ~ 5 for start_y in range(-len(key)+1, len(lock)): for start_x in range(-len(key)+1, len(lock[0])): # 자물쇠와 lock 일치하는 개수 ck = 0 match = True for y in range(start_y, st..