본문 바로가기
728x90

Python6

[백준]230410-230416 문제풀이(Python) 1774. 우주신과의 교감 import sys input = sys.stdin.readline def find(x): if parents[x] != x: parents[x] = find(parents[x]) return parents[x] def union(x, y): x = find(x) y = find(y) if x < y: parents[y] = x else: parents[x] = y N, M = map(int, input().split()) gods = [] for _ in range(N): r, c = map(int, input().split()) gods.append((r, c)) dist = [[0] * (N + 1) for _ in range(N + 1)] for i in range(N).. 2023. 4. 20.
[백준]230403-09 문제풀이(Python) 9328. 열쇠 from collections import deque import sys input = sys.stdin.readline dy = [-1, 1, 0, 0] dx = [0, 0, -1, 1] def bfs(y, x): global ans q = deque() q.append((y, x)) visited = [[False] * (w + 2) for _ in range(h + 2)] visited[y][x] = True while q: cnt = 0 now = q.popleft() for d in range(4): ny, nx = now[0] + dy[d], now[1] + dx[d] if ny h + 1 or nx w + 1: continue if .. 2023. 4. 9.
[SWEA]230404 문제풀이 최소 이동거리 def dijkstra(n, s): for i in adj[n]: nxt, d = i if distance[nxt] N - 1 or nx N - 1: continue fuel = 1 if lst[ny][nx] > lst[y][x]: fuel += lst[ny][nx] - lst[y][x] if distance[ny][nx] > distance[y][x] + fuel: distance[ny][nx] = distance[y][x] + fuel heapq.heappush(q, (ny, nx)) ans = distance[-1][-1] print(f'#{tc} {ans}') 01BFS에서 높이 차이에 따른 연료의 소모량까지 고려해줘야 하는 문제. 마찬가지로 효율적인 경로를 우.. 2023. 4. 4.
[백준]230327-0402 문제풀이 21278. 호석이 두마리 치킨 from collections import deque import sys input = sys.stdin.readline N, M = map(int, input().split()) adj = [[0] * (N + 1) for _ in range(N + 1)] ans = (0, 0, 1e10) for i in range(M): a, b = map(int, input().split()) adj[a][b] = adj[b][a] = 1 def bfs(n): q = deque() q.append((n, 1)) visited = [n] while q: now, c = q.popleft() for nxt in range(1, N + 1): if adj[now][nxt] == 1 and.. 2023. 4. 3.
[백준]230313-19 문제풀이 1107. 리모컨 import sys input = sys.stdin.readline def dfs(i, c): if i == len(str(N)): lst.append(c) return cnt = 0 now = int(str(N)[i]) while cnt 0 and now - cnt in fine: dfs(i + 1, c + str(now - cnt)) if now < 9 and now + cnt in fine: dfs(i + 1, c + str(now + cnt)) cnt += 1 N = int(input()) M = int(input()) broken = list(map(int, input().split())) fine = [] for i in range(10): if i.. 2023. 3. 21.
[SWEA]230112 문제풀이 1204. 최빈수 구하기 T = int(input()) for i in range(T): t_num = int(input()) t_case = list(input().split()) countnum = 0 num = 0 for k in range(1000): if t_case.count(t_case[k]) >= countnum: countnum = t_case.count(t_case[k]) num = t_case[k] print(f'#{t_num} {num}') 변수를 재선언해 최빈수 중 최댓값을 구하는 방식 계속 알고리즘을 헷갈려 런타임 오류가 난다 1209. sum for i in range(10): t_num = int(input()) t_case = [] for j in range(100): t .. 2023. 1. 12.
728x90