목록Python (42)
눈송이의 개발생활
문제 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..
문제 https://www.acmicpc.net/problem/15988 코드 import sys input = lambda : sys.stdin.readline().strip() n = int(input()) dp = [0] * 1000001 dp[1], dp[2], dp[3] = 1, 2, 4 for i in range(4, 1000001): dp[i] = dp[i - 1] % 1000000009 + dp[i - 2] % 1000000009 + dp[i - 3] % 1000000009 for _ in range(n): num = int(input()) print(dp[num] % 1000000009) 풀이 1번째는 메모리 초과, 2번째 시도는 시간초과로 틀린 문제이다 메모리 초과는 왜 나는지 계속 찾..