티스토리 뷰
https://www.acmicpc.net/problem/12869
12869번: 뮤탈리스크
1, 3, 2 순서대로 공격을 하면, 남은 체력은 (12-9, 10-1, 4-3) = (3, 9, 1)이다. 2, 1, 3 순서대로 공격을 하면, 남은 체력은 (0, 0, 0)이다.
www.acmicpc.net
메모이제이션을 활용해 DP로 풀이하였습니다.
C++ 소스 코드
#include <iostream>
#include <algorithm>
using namespace std;
int cache[61][61][61];
int dp(int s1, int s2, int s3){
if (s1 <0) return dp(0,s2,s3);
if (s2 <0) return dp(s1,0,s3);
if (s3 < 0) return dp(s1,s2,0);
if(s1 ==0 && s2 ==0 && s3==0) return 0;
if(cache[s1][s2][s3]!=-1) return cache[s1][s2][s3];
int t[] = {1,3,9};
int ret = -1;
do {
int tmp = dp(s1-t[0], s2-t[1], s3-t[2])+1;
if (ret == -1 || ret > tmp) ret = tmp;
}while(next_permutation(t,t+3));
return cache[s1][s2][s3] = ret;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
fill(&cache[0][0][0], &cache[60][60][60]+1,-1);
int scv[3] = {0,0,0};
for(int i=0;i<n;i++){
cin >> scv[i];
}
cout << dp(scv[0], scv[1], scv[2]) << '\n';
return 0;
}
'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글
BOJ) 17837 - 새로운 게임 2 (0) | 2020.10.15 |
---|---|
BOJ) 1389 - 케빈 베이컨의 6단계 법칙 (0) | 2020.10.08 |
BOJ) 2206 - 벽 부수고 이동하기 (0) | 2020.10.05 |
BOJ) 7569 - 토마토 (0) | 2020.09.17 |
BOJ) 11060 - 점프 점프 (0) | 2020.09.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- coreml
- 프로그래머스
- SwiftUI
- GraphDB
- blendshapes
- rxswift6
- mergesort
- Neo4j
- blendshape
- ARKit
- Swift
- 백준온라인저지
- 안드로이드
- Lottie
- infallible
- boj
- C++
- 백준
- 알고리즘
- 카카오인턴십
- 코코아팟
- ios
- SWEA
- Kotlin
- Reactivex
- disposeBag
- bounds
- cocoapods
- DispatchQueue
- rxswift
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함