코테/Algorithm & 방식

from collections import dequedef bfs(graph, start, visited): queue = deque([start]) visited[start] = True while queue: v = queue.popleft() print(v, end=" ") for i in graph[v]: if not visited[i]: queue.append(i) visited[i] = True#각 노드 번호에 연결 되어 있는 노드의 숫자graph = [ [], [2, 3, 8], [1, 7], [1, 4, 5], [3, 5], [3, ..
def dfs(graph, v, visited): visited[v] = True print(v, end = ' ') for i in graph[v]: if not visited[i]: dfs(graph, i, visited)# 각 노드 번호에 연결 되어 있는 노드의 숫자graph = [ [], [2, 3, 8], [1, 7], [1, 4, 5], [3, 5], [3, 4], [7], [2, 6, 8], [1, 7]]# 정렬이 안되어 있으면 정렬이 필요함for i in graph: i.sort() visited = [False] * 9dfs(graph, 1, visited)
graph = [[] for _ in range(3)]graph[0].append((1, 7))graph[0].append((2, 5))graph[1].append((0, 7,))print(graph)
str변환 활용 [브루트 포스]n = int(input())count = 0for i in range(n + 1): for j in range(60): for k in range(60): if '3' in str(i) + str(j) + str(k): count += 1print(count)  int형만 사용 [경우의 수 줄이기]n = int(input())count = 0for hour in range(n + 1): if hour % 10 == 3 or hour // 10 == 3: count += 60*60 continue for min in range(60): if min % 10 == ..
상하좌우 [ y, x 분리 ]n = int(input())a = list(input().split())posX, posY = 1, 1move = ['L', 'R', 'U', 'D']dx = [-1, 1, 0, 0]dy = [0, 0, -1, 1]for i in a: k = move.index(i) if posX + dx[k]    왕실의 나이트 [ y, x 형식 ]current_pos = input()row = int(current_pos[1])col = int(ord(current_pos[0]) - int(ord('a'))) + 1count = 0steps = [(-2, -1), (-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2),(-1, -2)]for..
류가든
'코테/Algorithm & 방식' 카테고리의 글 목록 (2 Page)