목록Algorithm/BOJ (35)
눈송이의 개발생활
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/c5q6HX/btrLjiOmue8/6gI9Rf5MjFpKstdHMe8O30/img.png)
문제 https://www.acmicpc.net/problem/2467 코드 import sys input = lambda : sys.stdin.readline().strip() n = int(input()) arr = list(map(int, input().split())) min_n = sys.maxsize ans_s, ans_e = 0, len(arr)-1 s, e = 0, len(arr)-1 while s 0: e -= 1 elif summ < 0: s += 1 else: break print(arr[ans_s], arr[a..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/ciWSSC/btruk47y8lr/bQNXPulNlkXKfdbaaDobxk/img.png)
문제 https://www.acmicpc.net/problem/1991 코드 import sys input = lambda : sys.stdin.readline().strip() def preorder(root): if root == '.': return print(root, end="") preorder(tree[root][0]) preorder(tree[root][1]) def inorder(root): if root == '.': return inorder(tree[root][0]) print(root, end="") inorder(tree[root][1]) def postorder(root): if root == '.': return postorder(tree[root][0]) postorder(..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/bsDvXh/btrtTLAyY0D/mrmHedSQZlkWtBzkfT0bn1/img.png)
문제 https://www.acmicpc.net/problem/11725 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def bfs(): queue = deque() queue.append(1) while queue: node = queue.popleft() for i in graph[node]: if parent[i] == 0: parent[i] = node queue.append(i) N = int(input()) graph = [[] for _ in range(N+1)] parent = [0 for _ in range(N+1)] for _ in range(N-1): a, b = map..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/dmc5ab/btrtNQCDLbb/0wWo9rIjrP8pKcWSJpirN0/img.png)
문제 https://www.acmicpc.net/problem/7576 코드 from collections import deque import sys input = lambda: sys.stdin.readline().strip() def bfs(): day = 0 while queue: day += 1 for _ in range(len(queue)): x, y = queue.popleft() for dx, dy in [(-1, 0), (1, 0), (0, 1), (0, -1)]: nx, ny = x + dx, y + dy if 0
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/bFYgUb/btrtDEu7Nfd/47iUHrMYLto2edHOulB5x0/img.png)
문제 https://www.acmicpc.net/problem/14226 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def bfs(s, c): queue = deque() queue.append((s, c)) while queue: s, c = queue.popleft() if dist[s][s] == -1: #방문하지 않은 경우, 화면에서 클립보드로 dist[s][s] = dist[s][c] + 1 queue.append((s, s)) if s+c = 0 and dist[s-1][c] == -1: #삭제 dist[s-1][c] = dist[s][c] + 1 queue.append((s-..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/nr3mx/btrtt5Aw3hh/ajM8aNsBrO0QQqvcc7Ct4k/img.png)
문제 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..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/be8fHc/btrtk2csx60/bndHgSKkPHol3pH4euqtmK/img.png)
문제 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
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/dVxLLR/btrtln8lYmb/sZAjOePYfH35wepPfzc0Ik/img.png)
문제 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