본문 바로가기
728x90
반응형

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

백준 2468 *코드 import sys from collections import deque sys.setrecursionlimit(10**6) input = sys.stdin.readline n = int(input()) # 초기 배열 array = [list(map(int, input().split())) for _ in range(n)] # 최대수 maxNumber = max(map(max, array)) # 임시 배열 temp_array = [[0] * n for _ in range(n)] # 물에 잠기는 영역 구분 def area(number): for i in range(n): for j in range(n): if array[i][j] < number: temp_array[i][j] = 1 # 물 잠김 .. 2023. 5. 22.
백준 2667 #2 재귀함수를 사용한 두 번째 풀이. 코드 길이 단축. def dfs(x, y): global count if x = N or y = N or grid[x][y] == 0: return grid[x][y] = 0 # 방문한 집은 0으로 표시 count += 1 dfs(x-1, y) # 상 dfs(x+1, y) # 하 dfs(x, y-1) # 좌 dfs(x, y+1) # 우 N = int(input()) grid = [list(map(int, input().strip())) for _ in range(N)] counts = [] for i in range(N): for j in range(N): if grid[i][j] == 1: count = 0 dfs(i, j) coun.. 2023. 5. 17.
백준 2798 #2 두 번째 풀이 시간은 아니지만 코드 길이는 단축되었다. import itertools a = list(map(int, input().split())) b = list(map(int, input().split())) combinations = itertools.combinations(b, 3) answer=[] for c in combinations: temp = 0 for n in range(3): temp += c[n] answer.append(temp) answer2=[] for n in answer: if n 2023. 5. 17.
백준 10819 #2 2번째 풀이 시간과 코드 길이 모두 단축되었다. import itertools a = int(input()) b = list(map(int, input().split())) permutations = itertools.permutations(b) answer = [] for p in permutations: temp_sum = 0 for n in range(1, a): ans = abs(p[n-1] - p[n]) temp_sum += ans answer.append(temp_sum) max_sum = max(answer) print(max_sum) 2023. 5. 17.
백준 2294 코드 n, k = map(int, input().split()) coin_list = [] for i in range(n): coin_list.append(int(input())) dp = [10001] * (k+1) dp[0] = 0 for i in range(1, k+1): for coin in coin_list: if coin 2023. 4. 13.
백준 2293 코드 n, k = map(int, input().split()) coins = [] for i in range(n): coins.append(int(input())) D = [0] * (k+1) D[0] = 1 for i in range(n): for j in range(coins[i], k+1): D[j] += D[j-coins[i]] print(D[k]) 2023. 4. 11.
728x90
반응형