Skip to content

Commit

Permalink
Merge pull request #47 from AlgoLeadMe/13-kokeunho
Browse files Browse the repository at this point in the history
13-kokeunho
  • Loading branch information
kokeunho authored Jan 11, 2025
2 parents b56da47 + 9e53247 commit 31f1798
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions kokeunho/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
| 10μ°¨μ‹œ | 2024.11.30 | 자료ꡬ쑰 | [κ°€μš΄λ°λ₯Ό λ§ν•΄μš”](https://www.acmicpc.net/problem/1655) | [#36](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/36) |
| 11μ°¨μ‹œ | 2024.12.21 | 그리디 μ•Œκ³ λ¦¬μ¦˜ | [νšŒμ˜μ‹€ λ°°μ •](https://www.acmicpc.net/problem/1931) | [#43](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/43) |
| 12μ°¨μ‹œ | 2024.12.25 | 그리디 μ•Œκ³ λ¦¬μ¦˜ | [크게 λ§Œλ“€κΈ°](https://www.acmicpc.net/problem/2812) | [#45](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/45) |
| 13μ°¨μ‹œ | 2024.12.30 | κ·Έλž˜ν”„ 탐색 | [μˆ¨λ°”κΌ­μ§ˆ](https://www.acmicpc.net/problem/1697) | [#47](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/47) |
---
35 changes: 35 additions & 0 deletions kokeunho/κ·Έλž˜ν”„ 탐색/13-kokeunho.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.example;

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();
int k = sc.nextInt();
int[] visited = new int[100001];
Arrays.fill(visited, -1);

Queue<Integer> queue = new LinkedList<>();
queue.add(n);
visited[n] = 0;

while(!queue.isEmpty()) {
int current = queue.poll();

if(current == k) {
System.out.println(visited[current]);
break;
}

for(int next : new int[]{current - 1, current + 1, current * 2}) {
if(next >= 0 && next <= 100000 && visited[next] == -1) {
visited[next] = visited[current] + 1;
queue.add(next);
}
}

}
}
}

0 comments on commit 31f1798

Please sign in to comment.