본문 바로가기

Algorithm245

[백준] 2206번 - 벽 부수고 이동하기 (파이썬) # 벽 부수고 이동하기 import sys from collections import deque input = sys.stdin.readline N, M = map(int, input().split()) arr = [] for i in range(N): arr.append(list(map(int, input().strip()))) dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] def bfs(): global result q = deque() q.append((0, 0, 0)) time = [[[0] * 2 for _ in range(M)] for _ in range(N)] time[0][0][0] = 1 while q: x, y, wall = q.popleft() if x == N-.. 2022. 1. 23.
[백준] 1987번 - 알파벳 (파이썬) # 알파벳 import sys input = sys.stdin.readline R, C = map(int, input().split()) arr = [] for _ in range(R): arr.append(list(input().strip())) dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] result = 0 def move(): global result q = set([(0, 0, arr[0][0])]) while q: x, y, hist = q.pop() result = max(result, len(hist)) for k in range(4): nx = x + dx[k] ny = y + dy[k] if 0 2022. 1. 23.
[백준] 11559번 - Puyo Puyo (파이썬) # Puyo Puyo from collections import deque import sys input = sys.stdin.readline N = 12 M = 6 dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] arr = [] for _ in range(12): arr.append(list(input().strip())) result = 0 def check(): isExplode = False global result visited = [[False] * M for _ in range(N)] for i in range(N): for j in range(M): if arr[i][j] != '.' and not visited[i][j]: color = arr[i][j] exp = [.. 2022. 1. 22.
[백준] 1062번 - 가르침 (파이썬) # 가르침 import sys input = sys.stdin.readline N, K = map(int, input().split()) arr = [] for _ in range(N): arr.append(input().strip()) alpha = ['a', 'n', 't', 'i', 'c'] alpha_list = ['b', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'o', 'p', 'q', 'r', 's', 'u', 'v', 'w', 'x', 'y', 'z'] def choose_alpha(n, start): global result if n == 0: result = max(result, check()) return for i in range(start, .. 2022. 1. 22.
[백준] 2638번 - 치즈 (파이썬) # 치즈 import sys from collections import deque import copy input = sys.stdin.readline dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] def air_check(arr): q = deque() q.append((0, 0)) arr[0][0] = 2 while q: x, y = q.popleft() for k in range(4): nx = x + dx[k] ny = y + dy[k] if 0 2022. 1. 22.
[백준] 1600번 - 말이 되고픈 원숭이 (파이썬) # 말이 되고픈 원숭이 from collections import deque import sys input = sys.stdin.readline monkey_dx = [0, 1, 0, -1] monkey_dy = [1, 0, -1, 0] horse_dx = [-2, -1, 1, 2, 2, 1, -1, -2] horse_dy = [1, 2, 2, 1, -1, -2, -2, -1] def bfs(n): q = deque() q.append((0, 0, n)) count = [[[0] * (n + 1) for _ in range(M)] for _ in range(N)] while q: x, y, K = q.popleft() if x == N-1 and y == M-1: return count[x][y][K] .. 2022. 1. 21.
[백준] 1647번 - 도시 분할 계획 (파이썬) MST, 프림 1. 값을 인접리스트로 받는다(양방향) 2. q에 초기값을 놓고(아무 노드) q가 빌때까지 - 현재 그래프에 연결이 안된 경우에만 (connected == False) - 연결을 해주고(connected == True) total에 비용(cost) append - 인접리스트를 for문으로 돌면서 연결이 안된 노드만 heapq에 heappush 3. max_value = total에서 가장 큰 값 4. 마을을 두곳으로 나눠야 해서 가장 비용이 큰 max_value를 sum(total)에서 빼준다. MST문제이며 크루스칼알고리즘으로도 풀 수 있다. 프림알고리즘으로 풀었다. 2022. 1. 21.