File tree Expand file tree Collapse file tree 5 files changed +128
-0
lines changed
Expand file tree Collapse file tree 5 files changed +128
-0
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <module type =" JAVA_MODULE" version =" 4" >
3+ <component name =" NewModuleRootManager" inherit-compiler-output =" true" >
4+ <exclude-output />
5+ <content url =" file://$MODULE_DIR$" >
6+ <sourceFolder url =" file://$MODULE_DIR$/src" isTestSource =" false" />
7+ </content >
8+ <orderEntry type =" inheritedJdk" />
9+ <orderEntry type =" sourceFolder" forTests =" false" />
10+ </component >
11+ </module >
Original file line number Diff line number Diff line change 1+ import java .util .Deque ;
2+ import java .util .LinkedList ;
3+ import java .util .Scanner ;
4+
5+ public class Main {
6+ static int [] visited ;
7+ static int minTime = (int ) 1e9 ;
8+
9+ public static void main (String [] args ) {
10+ Scanner scanner = new Scanner (System .in );
11+ visited = new int [100001 ];
12+
13+ int N = scanner .nextInt ();
14+ int K = scanner .nextInt ();
15+
16+ BFS (N , K );
17+ System .out .println (minTime );
18+ }
19+
20+ private static void BFS (int n , int k ) {
21+ Deque <int []> dq = new LinkedList <>();
22+ dq .offer (new int []{n , 0 });
23+ while (!dq .isEmpty ()) {
24+ int [] now = dq .pollFirst ();
25+ int place = now [0 ];
26+ int time = now [1 ];
27+ visited [place ] = 1 ;
28+ if (place == k ) {
29+ minTime = Math .min (minTime , time );
30+
31+ }
32+ if (0 <=place -1 && visited [place -1 ]==0 ){
33+ dq .offer (new int []{place -1 , time +1 });
34+ }
35+ if (place +1 <100001 && visited [place +1 ]==0 ){
36+ dq .offer (new int []{place +1 , time +1 });
37+ }
38+ if (place *2 <100001 && visited [place *2 ]==0 ){
39+ dq .offer (new int []{place *2 , time });
40+ }
41+
42+ }
43+
44+ }
45+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <module type =" JAVA_MODULE" version =" 4" >
3+ <component name =" NewModuleRootManager" inherit-compiler-output =" true" >
4+ <exclude-output />
5+ <content url =" file://$MODULE_DIR$" >
6+ <sourceFolder url =" file://$MODULE_DIR$/src" isTestSource =" false" />
7+ </content >
8+ <orderEntry type =" inheritedJdk" />
9+ <orderEntry type =" sourceFolder" forTests =" false" />
10+ </component >
11+ </module >
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ 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 ];
13+ 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 --){
18+ System .out .print (result .get (i )+" " );
19+ }
20+ }
21+
22+ private static int BFS (int n , int k ) {
23+ 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 ();
28+ int place = now [0 ];
29+ int time = now [1 ];
30+
31+ if (place ==k ){
32+ int idx =place ;
33+ while (idx !=n ){
34+ if (idx == n ){
35+ break ;
36+ }
37+ result .add (idx );
38+ idx =visited [idx ];
39+ }
40+ result .add (n );
41+ return time ;
42+
43+ }
44+ if (0 <=place -1 && visited [place -1 ]==-1 ){
45+ dq .offer (new int []{place -1 ,time +1 });
46+ visited [place -1 ]=place ;
47+ }
48+ if (place +1 <100001 && visited [place +1 ]==-1 ){
49+ dq .offer (new int []{place +1 ,time +1 });
50+ visited [place +1 ]=place ;
51+ }
52+ if (place *2 <100001 && visited [place *2 ]==-1 ){
53+ dq .offer (new int []{place *2 ,time +1 });
54+ visited [place *2 ]=place ;
55+ }
56+ }
57+ return n ;
58+ }
59+ }
You can’t perform that action at this time.
0 commit comments