Algorithm/BOJ133 [백준] 12581번 - 숨바꼭질 2 (파이썬) # 숨바꼭질 2 from collections import deque def bfs(start): global result q = deque() q.append(start) arr[start] = 0 while q: x = q.popleft() if x == K: result += 1 continue for nx in (x*2, x+1, x-1): # 최소시간이 arr[K]에 이미 있는 경우에는 같을 때도 # q.append해서 result가 증가될 수 있도록 if 0 2022. 1. 21. [백준] 17142번 - 연구소 3 (파이썬) # 연구소 3 import sys import copy from collections import deque input = sys.stdin.readline MAX = sys.maxsize dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] result = MAX def bfs(arr): global result time = [[0] * N for _ in range(N)] q = deque() for i in range(N): for j in range(N): if arr[i][j] == 'a': q.append((i, j)) time[i][j] = 1 while q: x, y = q.popleft() for k in range(4): nx = x + dx[k] ny = y + dy[k.. 2022. 1. 20. [백준] 4485번 - 녹색 옷 입은 애가 젤다지? (파이썬) # 녹색 옷 입은 애가 젤다지? from collections import deque import sys input = sys.stdin.readline MAX = sys.maxsize dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] def bfs(): q = deque() q.append((0, 0)) visit[0][0] = arr[0][0] while q: x, y = q.popleft() for k in range(4): nx = x + dx[k] ny = y + dy[k] if 0 2022. 1. 20. [백준] 1325번 - 효율적인 해킹 (파이썬) # 효율적인 해킹 import sys from collections import deque input = sys.stdin.readline def count(where): q = deque() q.append(where) visited = [False] * (N+1) visited[where] = True cnt = 1 while q: node = q.popleft() for nnode in arr[node]: if not visited[nnode]: q.append(nnode) cnt += 1 visited[nnode] = True return cnt N, M = map(int, input().split()) arr = [[] for _ in range(N+1)] for _ in range(M): a,.. 2022. 1. 20. [백준] 2178번 - 미로 탐색 (파이썬) # 미로 탐색 import sys from collections import deque input = sys.stdin.readline def go(x, y): q = deque() q.append((x, y)) while q: x, y = q.popleft() for i in range(4): nx = x + dx[i] ny = y + dy[i] if nx = N or ny = M: continue if arr[nx][ny] == 0: continue elif arr[nx][ny] == 1: q.append((nx, ny)) arr[nx][ny] = arr[x][y] + 1 N, M = map(int, input().split()) arr = [] for i in range(N): arr.append(.. 2022. 1. 20. [백분] 13913번 - 숨바꼭질 3 (파이썬) # 숨바꼭질 3 from collections import deque def bfs(): q = deque() q.append(N) while q: now = q.popleft() if now == M: return visited[now] for k in [now-1, now+1, now*2]: nnow = k if 0 2022. 1. 20. [백준] 1806번 - 부분합 (파이썬) # 부분합 import sys MAX = sys.maxsize input = sys.stdin.readline N, S = map(int, input().split()) arr = list(map(int, input().split())) end = 0 sum = 0 result = MAX for start in range(N): while end = S: result = min(result, end-start) sum -= arr[start] if result == MAX: print(0) else: print(result) 투포인터 1. start와 end로 연속되는 수열의 합(sum)을 구하고 목표값과 비교 2... 2022. 1. 20. 이전 1 ··· 16 17 18 19 다음