문제풀이/백준(Boj) 문제풀이 188

[백준][구현] 1009.분산처리 (파이썬/Python)

T = int(input()) for _ in range(T): a, b = map(int, input().split()) a = a % 10 if a == 0: print(10) elif a == 1 or a == 5 or a == 6: print(a) elif a == 4 or a == 9: b = b % 2 if b == 1: print(a) else: print((a * a) % 10) else: b = b % 4 if b == 0: print((a**4) % 10 % 10 % 10) else: print((a**b) % 10 % 10 % 10) # 거듭 제곱에 런타임에러가 나기 때문에 규칙을 잘 보면 답이 나오는 문제

[백준][Dijkstra/다익스트라] 4885. 녹색 옷 입은 애가 젤다지? (파이썬/Python)

import heapq import sys INF =int(1e9) input = sys.stdin.readline dx = [0,0,-1,1] dy = [1,-1,0,0] tastcase = 1 def dijsktra(): q = [] heapq.heappush(q,(graph[0][0],0,0)) #cost, 시작위치 graph[0][0]을 넣어줌 dist[0][0] = 0 while q: cost, x,y = heapq.heappop(q) if x == n-1 and y == n-1: #도착했다면? print(f"Problem {tastcase}: {dist[x][y]}") break for i in range(4): nx = x + dx[i] ny = y + dy[i] if 0

[백준][Floyd-Warshall/BFS]2660.회장 뽑기 (파이썬/Python)

from collections import deque n = int(input()) graph = [[] for _ in range(n+1)] score = [0] * (n+1) while True: a,b = map(int,input().split()) if a == -1 and b == -1: break graph[a].append(b) graph[b].append(a) def bfs(start): q = deque() q.append(start) while q: now = q.popleft() for i in graph[now]: if score[i] == 0: score[i] = score[now] + 1 q.append(i) bfs(1) min_score = min(score) print(min..

[백준][Greedy/BFS] 16953. A → B (파이썬/Python)

from collections import deque a,b = map(int,input().split()) q = deque() q.append((a,1)) # a부터 시작하는데 a부터 cnt가 1이기에 a,1을 큐에 넣어준다. while q: now, cnt = q.popleft() #종료 조건 if now == b: print(cnt) break elif now > b: continue q.append((int(str(now)+"1"),cnt+1)) # 맨오른쪽에 1을 추가해주는 것 q.append((now*2,cnt+1)) # 2를 곱하는 것 else: #큐를 모두 돌고 비어있는 큐인데도 원하는 값이 없으면 -1 출력 print(-1) a,b = map(int,input().split()) cnt..

[백준][DFS/BFS]2644. 촌수계산 (파이썬/Python)

import sys sys.setrecursionlimit(300000) input = sys.stdin.readline n = int(input()) start, end = map(int,input().split()) m = int(input()) graph = [[] for _ in range(n+1)] graph_dist = [0]*(n+1) for _ in range(m): u,v = map(int,input().split()) graph[u].append(v) graph[v].append(u) def dfs(start_node): for i in graph[start_node]: if graph_dist[i] == 0: graph_dist[i] = graph_dist[start_node] + 1..

[백준][DFS/BFS] 11725.트리의 부모 찾기 (파이썬/Python)

import sys input = sys.stdin.readline sys.setrecursionlimit(10**6) n = int(input()) graph = [[] for _ in range(n+1)] parentNode = [0 for _ in range(n+1)] for i in range(n-1): a,b = map(int,input().split()) graph[a].append(b) graph[b].append(a) def dfs(now): for i in graph[now]: if parentNode[i] == 0: parentNode[i] = now dfs(i) dfs(1) for i in parentNode[2:]: print(i) import sys input = sys.stdin..