-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptureThemAll.java
More file actions
71 lines (59 loc) · 1.73 KB
/
Copy pathcaptureThemAll.java
File metadata and controls
71 lines (59 loc) · 1.73 KB
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
/* The question can be found here.
https://community.topcoder.com/stat?c=problem_statement&pm=2915&rd=5853
*/
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
int findMinimum(String a, String b){
int xa = a.charAt(0) - 'a';
int xb = b.charAt(0) - 'a';
int ya = (a.charAt(1) - '0') - 1;
int yb = (b.charAt(1) - '0') - 1;
int dist[][]=new int[8][8];
/*Intialize the distance array with -1*/
for (int i=0; i <8;i++){
for (int j=0;j<8;j++){
dist[i][j]=-1;
}
}
int kJump[][] ={{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{1,-2},{-1,2},{1,2}};
dist[xa][ya]=0;
if(a==b){
return dist[xa][ya];
}
Queue<String> queue = new LinkedList<String>();
queue.add(Integer.toString(xa)+Integer.toString(ya));
while(!queue.isEmpty()){
String x = queue.poll();
xa = x.charAt(0) - '0';
ya = x.charAt(1) - '0';
for (int i=0; i<8;i++){
int xaa = xa + kJump[i][0];
int yaa = ya + kJump[i][1];
if (xaa<8 && xaa>=0 && yaa<8 && yaa>=0 ){ //&& dist[xaa][yaa] == -1
dist[xaa][yaa]=dist[xa][ya]+1;
if(xaa==xb && yaa==yb){
return dist[xaa][yaa];
}
queue.add(Integer.toString(xaa)+Integer.toString(yaa));
}
}
}
return -1;
}
public int fastKnight(String knight, String queen, String rook){
int mdistance = Math.min(findMinimum(knight, queen),findMinimum(knight, rook)) + findMinimum(queen, rook);
return mdistance;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Ideone id = new Ideone();
int d = id.fastKnight("b1","c3","a3");
System.out.println("d" +d);
}
}