Skip to content

Commit ad8022f

Browse files
committed
1115
1 parent af2c64d commit ad8022f

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

BOJ/gold/BOJ13913/src/Main.java

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,64 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
14
import java.util.*;
25

36
public class Main {
4-
static int [] visited;
5-
6-
static ArrayList<Integer> result;
7-
public static void main(String[] args) {
8-
9-
Scanner sc = new Scanner(System.in);
10-
int N = sc.nextInt();
11-
int K = sc.nextInt();
12-
visited = new int [100001];
7+
static ArrayList<Integer> result = new ArrayList<>();
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
String [] s = br.readLine().split(" ");
11+
int subin = Integer.parseInt(s[0]);
12+
int syster = Integer.parseInt(s[1]);
13+
int [] visited = new int [100001];
1314
Arrays.fill(visited, -1);
14-
result=new ArrayList<>();
15-
int time = BFS(N,K);
16-
System.out.println(time);
17-
for(int i= result.size()-1; i>=0; i--){
15+
int count= BFS(subin,syster,visited);
16+
System.out.println(count);
17+
for (int i = result.size()-1; i >=0 ; i--) {
1818
System.out.print(result.get(i)+" ");
1919
}
20+
21+
2022
}
2123

22-
private static int BFS(int n, int k) {
24+
private static int BFS(int subin, int syster, int[] visited) {
2325
Deque<int []> dq = new LinkedList<>();
24-
dq.offer(new int []{n,0});
25-
visited[n]=n;
26-
while (!dq.isEmpty()){
27-
int [] now = dq.pollFirst();
26+
dq.add(new int[]{subin,0});
27+
while (!dq.isEmpty()) {
28+
int[] now = dq.pollFirst();
2829
int place = now[0];
2930
int time = now[1];
30-
31-
if (place==k){
32-
int idx =place;
33-
while (idx!=n){
34-
if(idx == n){
31+
if (place == syster) {
32+
int idx = place;
33+
while (idx != subin) {
34+
if (idx == subin) {
3535
break;
3636
}
3737
result.add(idx);
38-
idx=visited[idx];
38+
idx = visited[idx];
3939
}
40-
result.add(n);
40+
result.add(idx);
4141
return time;
4242

43+
4344
}
44-
if (0<=place-1&& visited[place-1]==-1){
45-
dq.offer(new int []{place-1,time+1});
46-
visited[place-1]=place;
45+
if (place - 1 >= 0 && place - 1 < 100001 && visited[place - 1] == -1) {
46+
dq.add(new int[]{place - 1, time + 1});
47+
visited[place - 1] = place;
4748
}
48-
if (place+1<100001 && visited[place+1]==-1){
49-
dq.offer(new int []{place+1,time+1});
50-
visited[place+1]=place;
49+
if (place + 1 >= 0 && place + 1 < 100001 && visited[place + 1] == -1) {
50+
dq.add(new int[]{place + 1, time + 1});
51+
visited[place + 1] = place;
5152
}
52-
if(place*2<100001 && visited[place*2]==-1){
53-
dq.offer(new int []{place*2,time+1});
54-
visited[place*2]=place;
53+
if (place * 2 >= 0 && place * 2 < 100001 && visited[place * 2] == -1) {
54+
dq.add(new int[]{place * 2, time + 1});
55+
visited[place * 2] = place;
5556
}
57+
58+
5659
}
57-
return n;
60+
return -1;
5861
}
62+
63+
5964
}

0 commit comments

Comments
 (0)