Coding Test/알고스팟
[C++] 알고스팟/동적계획법 - 달팽이
Junchoi
2020. 8. 24. 14:00
문제 링크
https://algospot.com/judge/problem/read/SNAIL
algospot.com :: SNAIL
달팽이 문제 정보 문제 깊이가 n 미터인 우물의 맨 밑바닥에 달팽이가 있습니다. 이 달팽이는 우물의 맨 위까지 기어올라가고 싶어하는데, 달팽이의 움직임은 그 날의 날씨에 좌우됩니다. 만약 �
algospot.com
1. 현재날짜+1에서 한칸 더 올라가는 경우 * 0.25
2. 현재날짜+2에서 두칸 더 올라가는 경우 * 0.75
두가지 경우를 더해주면 값을 구할 수 있습니다.
구현 코드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <iomanip> | |
#define endl '\n' | |
#define fastio cin.sync_with_stdio(false); cin.tie(nullptr) | |
using namespace std; | |
const int MAX = 1001; | |
int n, m; | |
double cache[MAX][MAX]; | |
double climb(int days, int climbed){ | |
//m일이 지났을 때 n칸을 올라온 경우 1, 그렇지 않은 경우 0을 반환한다. | |
if(days == m) return climbed >= n ? 1 : 0; | |
double &ret = cache[days][climbed]; | |
if(ret != -1) return ret; | |
return ret = 0.25 * climb(days+1, climbed+1) + 0.75 * climb(days+1, climbed+2); | |
} | |
int main(){ | |
fastio; | |
int _; | |
cin >> _; | |
while(_--){ | |
fill_n(cache[0], MAX*MAX, -1); | |
cin >> n >> m; | |
cout << fixed; | |
cout.precision(10); | |
cout << climb(0,0) << endl; | |
} | |
return 0; | |
} |