티스토리 뷰
https://www.acmicpc.net/problem/11726
11726번: 2×n 타일링
2×n 크기의 직사각형을 1×2, 2×1 타일로 채우는 방법의 수를 구하는 프로그램을 작성하시오. 아래 그림은 2×5 크기의 직사각형을 채운 한 가지 방법의 예이다.
www.acmicpc.net
메모리제이션을 이용해 풀이했습니다.
C++ 소스 코드
#include <cstdio>
#define MOD 10007
using namespace std;
int d[1001],n;
long long dp(int x){
if(x==1) return d[x]=1;
else if (x==2) return d[x]=2;
else if(d[x]) return d[x];
return d[x]=(dp(x-1)+dp(x-2))%MOD;
}
int main(){
scanf("%d", &n);
printf("%lld\n",dp(n));
return 0;
}
Swift 소스 코드
import Foundation
func solution(){
var d = Array(repeating: 0, count: 1001)
let n = readLine().map({Int($0)!})!
func dp(x: Int) -> Int {
if x == 1 {
return 1
}
if x == 2 {
return 2
}
if d[x] != 0 {
return d[x]
}
d[x] = (dp(x: x-1) + dp(x: x-2))%10007
return d[x]
}
print(dp(x: n))
}
solution()
'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글
| BOJ) 15686 - 치킨 배달 (0) | 2020.06.13 |
|---|---|
| BOJ) 1261 - 알고스팟 (0) | 2020.06.12 |
| BOJ) 15684 - 사다리 조작 (0) | 2020.06.12 |
| BOJ) 15683 - 감시 (0) | 2020.06.11 |
| BOJ) 14891,15662 - 톱니바퀴 , 톱니바퀴 (2) (0) | 2020.06.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 카카오인턴십
- infallible
- rxswift6
- disposeBag
- blendshape
- Kotlin
- Swift unowned
- SwiftUI
- coreml
- Swift weak
- DispatchQueue
- C++
- 알고리즘
- 안드로이드
- Lottie
- SWEA
- UIHostingController
- GraphDB
- 코코아팟
- 프로그래머스
- Swift
- Reactivex
- boj
- cocoapods
- blendshapes
- ARKit
- rxswift
- 백준
- 백준온라인저지
- ios
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
글 보관함