File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static int [] visited = new int [100001 ];
7+ static int minTime = Integer . MAX_VALUE ;
8+ static int count = 0 ;
9+ static int start, target;
10+
11+ public static void main (String [] args ) throws IOException {
12+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
13+ StringTokenizer st = new StringTokenizer (br. readLine());
14+ start = Integer . parseInt(st. nextToken());
15+ target = Integer . parseInt(st. nextToken());
16+
17+ bfs();
18+
19+ System . out. println(minTime);
20+ System . out. println(count);
21+ }
22+
23+ public static void bfs () {
24+ Deque<Integer > queue = new ArrayDeque<> ();
25+ queue. add(start);
26+ visited[start] = 1 ;
27+
28+ while (! queue. isEmpty()) {
29+ int current = queue. poll();
30+ int time = visited[current];
31+
32+ if (current == target) {
33+ if (time - 1 < minTime) {
34+ minTime = time - 1 ;
35+ count = 1 ;
36+ } else if (time - 1 == minTime) {
37+ count++ ;
38+ }
39+ continue ;
40+ }
41+
42+ for (int next : new int []{current - 1 , current + 1 , current * 2 }) {
43+ if (next < 0 || next > 100000 ) continue ;
44+
45+ if (visited[next] == 0 || visited[next] == time + 1 ) {
46+ visited[next] = time + 1 ;
47+ queue. add(next);
48+ }
49+ }
50+ }
51+ }
52+ }
53+
54+ ```
You can’t perform that action at this time.
0 commit comments