|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Queue; |
| 6 | +import java.util.Set; |
| 7 | +
|
| 8 | +public class Main { |
| 9 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + private static Set<String> visited; |
| 12 | + private static int S; |
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + init(); |
| 15 | + int answer = BFS(); |
| 16 | +
|
| 17 | + bw.write(answer + "\n"); |
| 18 | + bw.flush(); |
| 19 | + bw.close(); |
| 20 | + br.close(); |
| 21 | + } |
| 22 | +
|
| 23 | + private static void init() throws IOException { |
| 24 | + S = Integer.parseInt(br.readLine()); |
| 25 | + visited = new HashSet<>(); |
| 26 | + } |
| 27 | +
|
| 28 | + private static int BFS() { |
| 29 | + Queue<int[]> q = new ArrayDeque<>(); |
| 30 | + int time = 0; |
| 31 | + visited.add(1+","+0); |
| 32 | + // 이모티콘 개수, 클립보드 개수, 시간 |
| 33 | + q.add(new int[]{1, 0, time}); |
| 34 | +
|
| 35 | + while (!q.isEmpty()) { |
| 36 | + int[] current = q.poll(); |
| 37 | + if (current[0] == S) { |
| 38 | + time = current[2]; |
| 39 | + break; |
| 40 | + } |
| 41 | +
|
| 42 | + for (int i = 0; i < 3; i++) { |
| 43 | + if (i == 0) { |
| 44 | + q.add(new int[]{current[0], current[0], current[2]+1}); |
| 45 | + } else if (i == 1 && current[1] > 0) { |
| 46 | + if (visited.contains(current[0]+current[1] + "," + current[1])) continue; |
| 47 | + visited.add(current[0]+current[1] + "," + current[1]); |
| 48 | + q.add(new int[]{current[0]+current[1], current[1], current[2]+1}); |
| 49 | + } else { |
| 50 | + if (current[0]-1<0 || visited.contains(current[0]-1 + "," + current[1])) continue; |
| 51 | + visited.add(current[0]-1 + "," + current[1]); |
| 52 | + q.add(new int[]{current[0]-1, current[1], current[2]+1}); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +
|
| 57 | + return time; |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
0 commit comments