본문 바로가기
728x90
반응형

알고리즘 문제풀이/백준22

백준 1463 코드 n = int(input()) d = [0] * (n+1) for i in range(2, n+1): d[i] = d[i-1] + 1 if i % 2 == 0 and d[i] > d[i//2] + 1: d[i] = d[i//2] + 1 if i % 3 == 0 and d[i] > d[i//3] + 1: d[i] = d[i//3] + 1 print(d[n]) 2023. 4. 8.
백준 16236 코드 from collections import deque n = int(input()) graph = [] for _ in range(n): graph.append(list(map(int, input().split()))) shark_size = 2 shark_x, shark_y = 0, 0 for i in range(n): for j in range(n): if graph[i][j] == 9: shark_x, shark_y = i, j graph[i][j] = 0 # 상어 위치는 빈칸으로 처리 break dx, dy = [-1, 0, 1, 0], [0, -1, 0, 1] # 북, 서, 남, 동 def bfs(): dist = [[-1] * n for _ in range(n)] q = deque([(s.. 2023. 4. 8.
백준 14502 코드 from collections import deque from copy import deepcopy import itertools N, M = map(int, input().split()) graph = [] for _ in range(N): graph.append(list(map(int, input().split()))) # 빈 칸의 좌표와 바이러스의 좌표를 구합니다. empty_spaces = [] virus_spaces = [] for i in range(N): for j in range(M): if graph[i][j] == 0: empty_spaces.append((i, j)) elif graph[i][j] == 2: virus_spaces.append((i, j)) # BFS로 바이러스가.. 2023. 4. 7.
백준 2178 처음에 시도한 코드(실패) from collections import deque n, m = map(int, input().split()) map_data = [list(map(int, input())) for m in range(n)] print(map_data) dx = [0, 0, -1, 1] dy = [1, -1, 0, 0] def bfs(x, y): queue = deque() queue.append([x, y]) count = 1 while queue: queue.popleft() for i in range(4): nx = x + dx[i] ny = y + dy[i] if nx m or ny > n: continue if map_data[nx][ny] =.. 2023. 4. 5.
백준 2667 DFS로 구현하려고 하니, n = int(input()) graph = [] for i in range(n): graph.append(list(map(int, input()))) print(graph) def dfs(x, y): if x = n: return False if graph[x][y] == 1: graph[x][y] == 0 dfs(x - 1, y) dfs(x, y - 1) dfs(x + 1, y) dfs(x, y + 1) return True return False result = 0 for i in range(n): for j in range(n): if dfs(i, j) == True: result += 1 print(result) RecursionError가 뜬다. BFS로 구현한 코드.. 2023. 4. 5.
백준 14888 import sys n = int(input()) numbers = list(map(int, input().split())) operators = list(map(int, input().split())) # 덧셈, 뺄셈, 곱셈, 나눗셈 개수 max_result = -sys.maxsize # 최댓값 초기화 min_result = sys.maxsize # 최솟값 초기화 def calculate(expression): # 주어진 수식의 결과를 계산하는 함수 stack = [] for e in expression: if e.isdigit(): stack.append(int(e)) else: b, a = stack.pop(), stack.pop() if e == '+': stack.append(a + b) eli.. 2023. 4. 4.
728x90
반응형