forked from learning-zone/java-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstepsByKnight.java
More file actions
140 lines (120 loc) · 2.66 KB
/
stepsByKnight.java
File metadata and controls
140 lines (120 loc) · 2.66 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package DynamicProgramming;
import java.util.*;
public class stepsByKnight {
public static int minSteps(Pair kp, Pair tp, int n, boolean[][] dp) {
int count = 0;
Queue<Pair> q = new LinkedList<>();
if (equals(kp, tp))
return count;
q.add(kp);
while (!q.isEmpty()) {
int size = q.size();
count++;
for (int i = 0; i < size; i++) {
Pair p = q.poll();
int x = p.x;
int y = p.y;
Pair newp = new Pair(x + 1, y - 2);
if (isValid(newp, n) && !dp[x + 1][y - 2]) {
if (equals(newp, tp)) {
return count;
} else {
q.add(newp);
dp[x + 1][y - 2] = true;
}
}
newp = new Pair(x + 1, y + 2);
if (isValid(newp, n) && !dp[x + 1][y + 2]) {
if (equals(newp, tp)) {
return count;
} else {
q.add(newp);
dp[x + 1][y + 2] = true;
}
}
newp = new Pair(x - 1, y - 2);
if (isValid(newp, n) && !dp[x - 1][y - 2]) {
if (equals(newp, tp)) {
return count;
} else {
q.add(newp);
dp[x - 1][y - 2] = true;
}
}
newp = new Pair(x - 1, y + 2);
if (isValid(newp, n) && !dp[x - 1][y + 2]) {
if (equals(newp, tp)) {
return count;
} else {
q.add(newp);
dp[x - 1][y + 2] = true;
}
}
newp = new Pair(x - 2, y + 1);
if (isValid(newp, n) && !dp[x - 2][y + 1]) {
if (equals(newp, tp)) {
return count;
} else {
q.add(newp);
dp[x - 2][y + 1] = true;
}
}
newp = new Pair(x - 2, y - 1);
if (isValid(newp, n) && !dp[x - 2][y - 1]) {
if (equals(newp, tp)) {
return count;
} else {
q.add(newp);
dp[x - 2][y - 1] = true;
}
}
newp = new Pair(x + 2, y - 1);
if (isValid(newp, n) && !dp[x + 2][y - 1]) {
if (equals(newp, tp)) {
return count;
} else {
q.add(newp);
dp[x + 2][y - 1] = true;
}
}
newp = new Pair(x + 2, y + 1);
if (isValid(newp, n) && !dp[x + 2][y + 1]) {
if (equals(newp, tp)) {
return count;
} else {
q.add(newp);
dp[x + 2][y + 1] = true;
}
}
}
}
return -1;
}
public static boolean equals(Pair p1, Pair p2) {
if (p1.x == p2.x && p1.y == p2.y)
return true;
return false;
}
public static boolean isValid(Pair p, int n) {
if (p.x >= 0 && p.x < n && p.y >= 0 && p.y < n) {
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Pair kp = new Pair(3, 4);
Pair tp = new Pair(0, 0);
boolean[][] dp = new boolean[6][6];
dp[3][4] = true;
System.out.println(minSteps(kp, tp, 6, dp));
}
}
class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}