-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS.java
117 lines (96 loc) · 3.02 KB
/
BFS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'catch_achilles' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER T
* 2. INTEGER A
*/
public static int catch_achilles(int T, int A) {
HashMap<Integer, Node> graph = new HashMap<Integer, Node>();
Queue<Node> q = new LinkedList<Node>();
Node root = new Node(T,null);
root.depth = 0;
graph.put(0, root);
q.add(root);
int key = 1;
while(!q.isEmpty()){
Node curr = q.poll();
int currDis = curr.value;
if(!q.contains(currDis*2)){
Node twoL = new Node(currDis*2, curr);
if(twoL.value == A){
graph.put(-1, twoL);
break;
}
else if(twoL.value < A+2){
graph.put(key++, twoL);
q.add(twoL);
}
}
if(!q.contains(currDis+1)){
Node plus = new Node(currDis+1, curr);
if(plus.value == A){
graph.put(-1, plus);
break;
}
else{
graph.put(key++, plus);
q.add(plus);
}
}
if(!q.contains(currDis-1)){
Node minus = new Node(currDis-1, curr);
if(minus.value == A){
graph.put(-1, minus);
break;
}
else if(minus.value > -2){
graph.put(key++, minus);
q.add(minus);
}
}
}
return graph.get(-1).depth;
}
}
class Node {
//String label;
int depth;
int value;
Node parent;
//ArrayList<Node> adjacencyList;
public Node(int invalue, Node par){
this.value = invalue;
if(par!=null){
this.depth = par.depth +1;
}
this.parent = par;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int T = Integer.parseInt(firstMultipleInput[0]);
int A = Integer.parseInt(firstMultipleInput[1]);
int mins = Result.catch_achilles(T, A);
bufferedWriter.write(String.valueOf(mins));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}