반응형
문제 출처 :
https://www.acmicpc.net/problem/1699
알고리즘 분석 :
문제 해결에 필요한 사항
1. Dynamic Programming
2. BFS
BFS로 문제를 해결할 수 있다.
for (int i = 0; i <= n; i++)
{
int there = here + i*i;
if (there > n)
break;
DP[there] = min(DP[there], DP[here] + 1);
q.push(there);
}
there은 현재 자리에서 제곱수만큼 뛰었을 때의 값을 의미한다.
DP[there] = min(DP[there], DP[here] + 1);
해당하는 위치에 이미 존재하는 값과 현재 위치에서 +1(제곱수만큼 뛸때 드는 비용)중 최소를 DP[there]에 넣는다.
소스 코드 :
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <cstdio> #include <iostream> #include <queue> #include <algorithm> #include <limits.h> #include <queue> using namespace std; int DP[100002]; bool visit[100002]; queue <int> q; int main() { int n; scanf("%d", &n); fill(DP, DP + 100002, 999); DP[0] = 0; q.push(0); while (!q.empty()) { int here = q.front(); q.pop(); if (visit[here]) continue; visit[here] = 1; for (int i = 0; i <= n; i++) { int there = here + i*i; if (there > n) break; DP[there] = min(DP[there], DP[here] + 1); q.push(there); } } //for(int i = 1; i <= n ; i++) printf("%d", DP[n]); return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[2644번] 촌수계산 (0) | 2017.03.06 |
---|---|
[11052번] 붕어빵 판매하기 (0) | 2017.03.06 |
[2957번] 이진 탐색 트리 (0) | 2017.03.06 |
[5637번] 가장 긴 단어 (2) | 2017.03.06 |
[2294번] 동전 2 (0) | 2017.03.06 |