File tree Expand file tree Collapse file tree 3 files changed +66
-0
lines changed
Expand file tree Collapse file tree 3 files changed +66
-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 TimeMin = (int ) 1e9 ;
7+ static int count =0 ;
8+ static int [] visited ;
9+ public static void main (String [] args ) {
10+
11+ Scanner sc = new Scanner (System .in );
12+ int N = sc .nextInt ();
13+ int K = sc .nextInt ();
14+ visited = new int [100001 ];
15+ BFS (N , K );
16+ System .out .println (TimeMin );
17+ System .out .println (count );
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+ int flag =0 ;
24+ while (!dq .isEmpty ()) {
25+ int [] now = dq .pollFirst ();
26+ int place = now [0 ];
27+ int time = now [1 ];
28+ visited [place ]=1 ;
29+
30+ if (place == k ) {
31+ if (flag ==0 ){
32+ TimeMin = time ;
33+ count ++;
34+ flag =1 ;
35+ }else if (flag ==1 && TimeMin ==time ){
36+ count ++;
37+ }
38+
39+
40+ }
41+ if (0 <= place - 1 && visited [place -1 ]==0 ) {
42+ dq .offer (new int []{place - 1 , time + 1 });
43+ }
44+ if (place +1 < 100001 && visited [place +1 ]==0 ) {
45+ dq .offer (new int []{place + 1 , time + 1 });
46+ }
47+ if (place *2 < 100001 && visited [place *2 ]==0 ){
48+ dq .offer (new int []{place * 2 , time + 1 });
49+ }
50+
51+
52+ }
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments