14226번: 이모티콘
영선이는 매우 기쁘기 때문에, 효빈이에게 스마일 이모티콘을 S개 보내려고 한다. 영선이는 이미 화면에 이모티콘 1개를 입력했다. 이제, 다음과 같은 3가지 연산만 사용해서 이모티콘을 S개 만
www.acmicpc.net
#include <iostream>
#include <tuple>
#include <queue>
#include <cstring>
using namespace std;
int d[1001][1001];
int main() {
int n;
cin >> n;
memset(d,-1,sizeof(d));
queue<pair<int,int>> q;
q.push(make_pair(1,0));
d[1][0] = 0;
while (!q.empty()) {
int s, c;
tie(s, c) = q.front();
q.pop();
if (d[s][s] == -1) {
d[s][s] = d[s][c] + 1;
q.push(make_pair(s,s));
}
if (s+c <= n && d[s+c][c] == -1) {
d[s+c][c] = d[s][c] + 1;
q.push(make_pair(s+c, c));
}
if (s-1 >= 0 && d[s-1][c] == -1) {
d[s-1][c] = d[s][c] + 1;
q.push(make_pair(s-1, c));
}
}
int ans = -1;
for (int i=0; i<=n; i++) {
if (d[n][i] != -1) {
if (ans == -1 || ans > d[n][i]) {
ans = d[n][i];
}
}
}
cout << ans << '\n';
return 0;
}
'Problem set' 카테고리의 다른 글
[백준] 1261 알고스팟 (0) | 2021.02.20 |
---|---|
[백준] 13549 숨박꼭질 3 (0) | 2021.02.20 |
[백준] 13913 숨바꼭질 4 (0) | 2021.02.20 |
[백준] 1697 숨바꼭질 (0) | 2021.02.20 |
[백준] 2146 다리 만들기 (0) | 2021.02.20 |