목록Algorithm (55)
눈송이의 개발생활

문제 https://www.acmicpc.net/problem/13023 코드 import sys input = lambda: sys.stdin.readline().strip() def dfs(v, depth): global ans visited[v] = 1 if depth == 4: ans = True return for i in graph[v]: if visited[i] == 0: dfs(i, depth + 1) visited[i] = 0 N, M = map(int, input().split()) graph = [[] for _ in range(N)] visited = [0 for _ in range(N)] ans = False for _ in range(M): a, b = map(int, input..

문제 https://www.acmicpc.net/problem/13549 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() MAX = 100001 N, K = map(int, input().split()) visit_time = [-1] * MAX visit_time[N] = 0 queue = deque() queue.append(N) while queue: x = queue.popleft() if x == K: # 해당 수에 도착 print(visit_time[x]) break if 0

문제 https://www.acmicpc.net/problem/1697 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def bfs(): queue = deque() queue.append(N) while queue: x = queue.popleft() if x == K: # 해당 수에 도착 print(visited[x]) break for nx in (x - 1, x + 1, x * 2): if 0

문제 https://www.acmicpc.net/problem/2178 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def bfs(x, y): queue = deque([(x, y)]) visited[x][y] = 1 while queue: cx, cy = queue.popleft() for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]: nx, ny = cx + dx, cy + dy if 0 > nx or nx > N-1 or ny M-1: continue if visited[nx][ny] == 0 and graph[nx][ny] =..

문제 https://www.acmicpc.net/problem/4963 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def bfs(x, y): queue = deque([(x, y)]) visited[x][y] = 1 while queue: cx, cy = queue.popleft() for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, 1), (1, -1), (1, 1), (-1, -1)]: nx, ny = cx + dx, cy + dy if nx h-1 or ny w-1 : continue if vis..

문제 https://www.acmicpc.net/problem/1260 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def dfs(v): print(v, end=' ') visited[v] = 1 for i in range(1, N+1): if visited[i] == 0 and graph[v][i] == 1: dfs(i) def bfs(v): visited = [0] * (N + 1) # BFS 위해서 초기화하기 queue = deque([v]) visited[v] = 1 while queue: k = queue.popleft() print(k, end=' ') for i in range..

문제 https://www.acmicpc.net/problem/11724 코드 import sys input = lambda : sys.stdin.readline().strip() sys.setrecursionlimit(10000) def dfs(v): visited[v] = 1 for i in range(1, N+1): # 연결되어 있고 visited 아니면 다시 탐색 if s[v][i] == 1 and visited[i] == 0: dfs(i) N, M = map(int, input().split()) s = [[0] * (N+1) for i in range(N+1)] visited = [0 for i in range(N+1)] count = 0 for i in range(M): a, b = map(..

문제 https://www.acmicpc.net/problem/11052 코드 import sys input = lambda : sys.stdin.readline().strip() n = int(input()) p = [0] + list(map(int, input().split())) dp = [0 for _ in range(n+1)] for i in range(1, n+1): for k in range(1, i+1): # dp를 max값으로 계속 갱신 dp[i] = max(p[k] + dp[i-k], dp[i]) print(dp[n]) 풀이 사실 처음에는 접근이 어려워서 힌트를 보고 풀었다🤔 점화식을 찾고 그 점화식에 맞는 코드를 짜서 풀 수 있었다. 문제에 있는 예시로 설명하자면, P1 = 1, P2..