반응형

https://www.acmicpc.net/problem/14226

 

14226번: 이모티콘

영선이는 매우 기쁘기 때문에, 효빈이에게 스마일 이모티콘을 S개 보내려고 한다. 영선이는 이미 화면에 이모티콘 1개를 입력했다. 이제, 다음과 같은 3가지 연산만 사용해서 이모티콘을 S개 만

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. S를 입력받고, visited[ cur ][ copy ] 배열을 통해 현재 수와 클립보드에 저장된 수의 방문 여부를 체크합니다.

 

2. 다음의 세 경우를 queue에 넣어줍니다.

  - 복사 q.push({ cur, cur, cnt + 1 })

  - 붙여넣기 q.push({ cur + copy, copy, cnt + 1 })

  - 지우기 q.push({ cur - 1, copy, cnt + 1 })

 

3. S와 cur이 일치하면 cnt를 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<queue>
 
using namespace std;
 
int S;
int visited[2001][2001];
 
struct node {
    int cur;
    int copy;
    int cnt;
};
 
int main()
{
    scanf("%d"&S);
 
    queue<node> q;
 
    q.push({ 1,0,0 });
 
    while (!q.empty()) {
        const int cur = q.front().cur;
        const int copy = q.front().copy;
        const int cnt = q.front().cnt;
        q.pop();
 
        if(cur > 2000continue;
        if (visited[cur][copy] == 1continue;
        visited[cur][copy] = 1;
 
        if (S == cur) {
            printf("%d", cnt);
            return 0;
        }
 
        q.push({ cur,cur,cnt + 1 });
        q.push({ cur + copy,copy,cnt + 1 });
        if (cur - 1 > 0) {
            q.push({ cur - 1,copy,cnt + 1 });
        }
    }
}
cs
반응형

+ Recent posts