Skip to content

Commit 8bc2fc9

Browse files
committed
231024
1 parent bc44a12 commit 8bc2fc9

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/graph/BOJ_14226_이모티콘.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package graph;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
import java.util.Scanner;
6+
7+
/**
8+
* [조건]
9+
* 이미 화면에 이모티콘 1개를 입력했다
10+
* 이제부터 다음 3가지 연산만 가능하다
11+
* 1. 화면에 있는 전체 이모티콘을 복사해서 클립보드에 저장
12+
* 2. 클립보드에 있는 모든 이모티콘을 화면에 붙여넣기
13+
* 3. 화면에 있는 이모티콘 중 하나를 삭제
14+
*
15+
* 모든 연산은 1초가 걸린다.
16+
* 클립보드에 이모티콘을 복사하면 이전에 있던 내용은 덮어쓰기가 된다
17+
* 클립보드가 비어있는 상태에서는 붙여넣기를 할 수 없다
18+
* 일부만 클립보드에 복사할 수는 없다
19+
*
20+
* 화면에 S개의 이모티콘을 만드는 데 걸리는 최솟값을 구하라
21+
*
22+
* [풀이]
23+
* 이모티콘의 개수만 맞추면 되므로 각 연산을 다음과 같이 단순화
24+
* 화면 상 이모티콘의 갯수: x, 클립보드 상 이모티콘의 갯수: y
25+
* 1. y = x
26+
* 2. x += y
27+
* 3. x -= 1
28+
*/
29+
public class BOJ_14226_이모티콘 {
30+
31+
public static void main(String[] args) {
32+
Scanner sc = new Scanner(System.in);
33+
34+
int S = sc.nextInt();
35+
System.out.println(solution(S));
36+
}
37+
38+
static int solution(int S) {
39+
Queue<int[]> que = new LinkedList<>();
40+
boolean[][] visited = new boolean[1001][1001];
41+
42+
que.add(new int[]{1, 0});
43+
visited[1][0] = true;
44+
45+
int cnt = 0;
46+
while (!que.isEmpty()) {
47+
int size = que.size();
48+
49+
while (size-- > 0) {
50+
int[] cur = que.poll();
51+
int d = cur[0], c = cur[1];
52+
53+
if (d == S) return cnt;
54+
55+
for (int i = 0; i < 3; i++) {
56+
int nd = d, nc = c;
57+
if (i == 0) { // 연산1. 화면에 있는 전체 이모티콘을 복사해서 클립보드에 저장
58+
nc = d;
59+
} else if (i == 1) { // 연산2. 클립보드에 있는 모든 이모티콘을 화면에 붙여넣기
60+
if (c == 0) continue; // 클립보드가 비어있는 경우 건너뛴다
61+
nd += c;
62+
} else { // 연산3. 화면에 있는 이모티콘 중 하나를 삭제
63+
if (d <= 0) continue;
64+
nd -= 1;
65+
}
66+
67+
if (nd > 1000 || nc > 1000) continue;
68+
if (visited[nd][nc]) continue; // 이미 방문한 위치(개수)인 경우
69+
visited[nd][nc] = true;
70+
que.add(new int[]{nd, nc});
71+
}
72+
}
73+
cnt++;
74+
}
75+
76+
return -1;
77+
}
78+
79+
}

0 commit comments

Comments
 (0)