[백준 바킹독] 0x09 - BFS 토마토(7576)
Introduction
기초적인 동적 계획법 문제들을 풀어봅시다.
https://www.acmicpc.net/step/16
점화식의 값을 특정 상수로 나눈 나머지를 구하는 문제
import sys
input = sys.stdin.readline
N = int(input())
# 1 하나만으로 이루어진 타일 또는 00 타일로 만들 수 있는 조합
# 1, 1 1
# 2, 00, 11 2
# 3, 001, 100, 111 3
# 4, 0000, 0011, 1100, 1001, 1111 5
# 5, 00001, 00100, 10000, 00111, 10011, 11001, 11100, 11111 8
# f(n) = f(n-1) + f(n-2)
dp = [0]*(N+1)
if N >= 1:
dp[1] = 1
if N >= 2:
dp[2] = 2
if N > 2:
for i in range(3,N+1):
dp[i] = (dp[i-1] + dp[i-2])%15746
print(dp[N])
house를 2차원 배열로 선언
ex) house[i]0, house[i]1, house[i]1
전체 점화식
점화식 요약
결과는 dp[i]의 값 중 최소값이다.
result = min(dp[i])
import sys
input = sys.stdin.readline
N = int(input())
house = []
for _ in range(N):
R, G, B = map(int,input().split())
house.append((R,G,B))
dp = [[0,0,0] for _ in range(N)]
dp[0][0], dp[0][1], dp[0][2] = house[0]
for i in range(N):
dp[i][0] = min(dp[i-1][1],dp[i-1][2]) + house[i][0]
dp[i][1] = min(dp[i-1][0],dp[i-1][2]) + house[i][1]
dp[i][2] = min(dp[i-1][0],dp[i-1][1]) + house[i][2]
print(min(dp[-1]))
RGB 거리 푼 것을 응용하여 풀이
n*n 2차원 배열로 dp 표현 (이중 반복문)
int_try = [[0]*n for _ in range(n)]
<!-- [[7, 0, 0, 0, 0],
[3, 8, 0, 0, 0],
[8, 1, 0, 0, 0],
[2, 7, 4, 4, 0],
[4, 5, 2, 6, 5]] -->
import sys
input = sys.stdin.readline
n = int(input())
int_tri = [[0]*n for _ in range(n)]
for idx in range(n):
numbers = list(map(int,input().split()))
for num in range(len(numbers)):
int_tri[idx][num] = numbers[num]
dp = [[0]*n for _ in range(n)]
dp[0][0] = int_tri[0][0]
for i in range(1,n):
for j in range(i+1):
if j == 0:
dp[i][j] = dp[i-1][j] + int_tri[i][j]
else:
dp[i][j] = max(dp[i-1][j-1], dp[i-1][j]) + int_tri[i][j]
print(max(dp[-1]))
첫번째 계단은 밟을 수도 있고 안 밟을 수도 있다.
마지막 계단은 반드시 밟이야한다.
점화식
import sys
input = sys.stdin.readline
n = int(input())
stair = []
for _ in range(n):
stair.append(int(input()))
# 점수 누적값
dp = [0]*n
if n >= 1:
dp[0] = stair[0]
if n >= 2:
dp[1] = max(stair[0]+stair[1],stair[1])
if n >= 3:
dp[2] = max(stair[0]+stair[2],stair[1]+stair[2])
if n >= 4:
for i in range(3,n):
dp[i] = max(dp[i-3]+stair[i-1],dp[i-2]) + stair[i]
print(dp[-1])
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
1167 트리의 지름
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Git 명령어를 사용한 하위 디렉토리 다운로드 Clone 할 로컬 저장소 생성
Introduction
# Fetch the submodule commits into the main repository git remote add submodule_origin git://url/to/submodule/origin git fetch submodule_origin
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Graph
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Spring Project
the page for java
가벼운 Base image를 사용
version: 3.0.0a10
WIDTH
version: 3.0.0a10
#include <iostream> #include <thread> #include <chrono> #include <mutex> #include <atomic> #include <string.h>
version: 3.0.0a10
https://cplusplus.com/reference/future/
Multithreading support was introduced in C++11.
how to costom github blog using jekyll
You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different wa...