와...완전 생각도 못하고 있었습니다.. 이거 예외처리하니까 75%에서 바로 넘어가네요 감사합니다...
아래가 제 코드인데 혹시 (n==k) 전처리 말고 좀 더 코드를 깔끔하게 한꺼번에 처리하도록 짜는 방법 없을까요?
import java.util.*;
import java.io.*;
public class P12851 {
private static BufferedReader <i>br</i>;
private static BufferedWriter <i>bw</i>;
private static StringTokenizer <i>st</i>;
private static int <i>n</i>;
private static int <i>k</i>;
private static int[] <i>arr</i>;
private static boolean[] <i>visited</i>;
private static int <i>cnt</i>;
public static void main(String[] args) throws IOException {
<i>br </i>= new BufferedReader(new InputStreamReader(System.<i>in</i>));
<i>bw </i>= new BufferedWriter(new OutputStreamWriter(System.<i>out</i>));
<i>solution</i>();
}
private static void init() throws IOException {
<i>st </i>= new StringTokenizer(<i>br</i>.readLine());
<i>n </i>= Integer.<i>parseInt</i>(<i>st</i>.nextToken());
<i>k </i>= Integer.<i>parseInt</i>(<i>st</i>.nextToken());
<i>arr </i>= new int[100001];
Arrays.<i>fill</i>(<i>arr</i>, Integer.<i>MAX_VALUE</i>);
<i>visited </i>= new boolean[100001];
<i>cnt </i>= 0;
}
private static void solution() throws IOException {
<i>init</i>();
if (<i>n </i>!= <i>k</i>) {
<i>bfs</i>();
} else {
<i>arr</i>[<i>k</i>] = 0;
<i>cnt </i>= 1;
}
<i>bw</i>.write(String.<i>valueOf</i>(<i>arr</i>[<i>k</i>]) + "\n");
<i>bw</i>.write(String.<i>valueOf</i>(<i>cnt</i>));
<i>bw</i>.flush();
<i>bw</i>.close();
<i>br</i>.close();
}
private static void bfs() {
Deque<Integer> q = new LinkedList<>();
q.add(<i>n</i>);
<i>arr</i>[<i>n</i>] = 0;
<i>visited</i>[<i>n</i>] = true;
while (!q.isEmpty()) {
int curX = q.remove();
for (int i = 0; i < 3; i++) {
int nextX = curX;
if (i == 0) {
nextX++;
} else if (i == 1) {
nextX--;
} else if (i == 2) {
nextX *= 2;
}
if (<i>isInRange</i>(nextX) && (!<i>visited</i>[nextX] || <i>arr</i>[curX] < <i>arr</i>[nextX])) {
if (nextX == <i>k</i>) {
<i>cnt</i>++;
<i>arr</i>[nextX] = <i>arr</i>[curX] + 1;
<i>visited</i>[nextX] = true;
continue;
}
q.add(nextX);
<i>arr</i>[nextX] = <i>arr</i>[curX] + 1;
<i>visited</i>[nextX] = true;
}
}
}
}
private static boolean isInRange(int x) {
if (0 <= x && x <= 100000) {
return true;
}
return false;
}
}

kangcombi 2년 전 1
N == K 일때
0 1이 출력되어야 합니다.