Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- baekjoon 16953
- 백준 섬의갯수
- baekjoon1012
- baekjoon 3187
- websecurityconfiguration
- coding test
- 백준 2667 java
- bytebuddyinterceptor
- beakjoon11725
- spring boot
- baekjoon 4963
- baekjoon 14714
- CodingTest
- 단지 번호붙이기
- exprectedbelow
- baekjoon 2667
- Error
- 백준 현수막
- backjoon 16173
- baekjoon
- attvalue
- springboot
- java.lang.nosuchmethoderror
- baekjoon 2468
- 안전영역 java
- dfs
- beakjoon 1697
- BFS
- html error
- beakjoon
Archives
- Today
- Total
NOT FOUND
[코테] 백준 16953 A->B | BFS Java 본문
https://www.acmicpc.net/problem/16953
테스트 케이스는 모두 잘 나오고, 게시판 반례들도 잘 나오는데 4%에서 틀렸다고 나왔다.
틀린 코드
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
// https://www.acmicpc.net/problem/16953
public class baekjoon_16953 {
private static int A, B;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
A = Integer.parseInt(st.nextToken()); // 기본 값
B = Integer.parseInt(st.nextToken()); // 변경할 값
bfs();
}
static void bfs(){
Queue<Point> que = new LinkedList<>();
que.add(new Point(A, 1));
while(!que.isEmpty()){
Point p = que.poll();
for(int i =0; i<2; i++){
int next = p.x;
if(i==0) {
next = next * 2;
} else {
next = next * 10 + 1;
}
if(next == B) {
System.out.println(p.y + 1);
return;
}
if(next < B){
que.add(new Point(next, p.y+1));
} else if (que.isEmpty()) {
System.out.println(-1);
return;
}
}
}
}
}
next 뒤에 1을 붙이는 걸 원래 next = Integer.parseInt(next + "1");로 했었는데 java.lang.NumberFormatException 오류가 나길래 수정했다.
틀린 부분
아무리 봐도 틀린 게 없었는데 게시판에서 이 글을 보았다.
https://www.acmicpc.net/board/view/158122

혹시나 해서 int -> long으로 변경. 성공했다.
입력값에는 문제가 없는데, 내가 next뒤에 1을 붙이면서 오버플로우가 발생한 것이었다!
최종 코드
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
// https://www.acmicpc.net/problem/16953
public class baekjoon_16953 {
private static long A, B;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
A = Integer.parseInt(st.nextToken()); // 기본 값
B = Integer.parseInt(st.nextToken()); // 변경할 값
bfs();
}
static void bfs(){
Queue<CustomPoint> que = new LinkedList<>();
que.add(new CustomPoint(A, 1));
while(!que.isEmpty()){
CustomPoint p = que.poll();
for(int i =0; i<2; i++){
long next = p.x;
if(i==0) {
next = next * 2;
} else {
next = next * 10 + 1;
}
if(next == B) {
System.out.println(p.y + 1);
return;
}
if(next < B){
que.add(new CustomPoint(next, p.y+1));
} else if (que.isEmpty()) {
System.out.println(-1);
return;
}
}
}
}
public static class CustomPoint {
long x;
long y;
public CustomPoint(long x, long y) {
this.x = x;
this.y = y;
}
}
}
혼자 힘으로 다 해결해서 뿌듯하다~ 어제 풀었던 거랑 비슷해서 금방 푼 것 같다
'coding test' 카테고리의 다른 글
| [코테] 백준 13565 침투: DFS Java (0) | 2025.05.28 |
|---|---|
| [코테] 백준 2468 안전 영역: DFS Java (1) | 2025.05.14 |
| [코테] 백준 2178 미로 탐색: BFS Java (0) | 2025.05.09 |
| [코테] 백준 11725 트리의 부모 찾기: BFS Java (0) | 2025.05.08 |
| [코테] 백준 2667 단지 번호붙이기: DFS Java (1) | 2025.05.07 |