-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinimumStepsByKnight.java
124 lines (111 loc) · 3.49 KB
/
MinimumStepsByKnight.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
118
119
120
121
122
123
124
/*https://practice.geeksforgeeks.org/problems/steps-by-knight5927/1/*/
class Cell implements Comparable<Cell>
{
int x;
int y;
int steps;
Cell(int x, int y, int steps)
{
this.x = x;
this.y = y;
this.steps = steps;
}
@Override
public int compareTo(Cell c)
{
return 0;
}
}
class Solution
{
boolean result;
int minSteps;
int n;
int destRow, destCol;
public boolean isSafe(int r, int c, boolean[][] visited) {
//if boundary conditions violate, return false
if (r <= -1 || r >= n || c <= -1 || c >= n)
return false;
//if the move is unsafe, return false
if (visited[r][c])
return false;
//return true
return true;
}
public void checkReachability(int srcRow, int srcCol, boolean[][] visited) {
Queue<Cell> queue = new LinkedList<Cell>();
queue.add(new Cell(srcRow,srcCol,0));
visited[srcRow][srcCol] = true;
int r = -1, c = -1, steps = -1;
while (!queue.isEmpty())
{
Cell currCell = queue.poll();
r = currCell.x;
c = currCell.y;
steps = currCell.steps;
//if conditions violate, return
if (r <= -1 || c <= -1 || r >= n || c >= n) return;
//if destination is reached, store true and return
if (r == destRow && c == destCol)
{
result = true;
minSteps = Math.min(minSteps,steps);
}
//move in all four directions (backtracking step-2)
if (isSafe(r+2,c+1,visited))
{
queue.add(new Cell(r+2,c+1,steps+1));
visited[r+2][c+1] = true;
}
if (isSafe(r+1,c+2,visited))
{
queue.add(new Cell(r+1,c+2,steps+1));
visited[r+1][c+2] = true;
}
if (isSafe(r-2,c+1,visited))
{
queue.add(new Cell(r-2,c+1,steps+1));
visited[r-2][c+1] = true;
}
if (isSafe(r+1,c-2,visited))
{
queue.add(new Cell(r+1,c-2,steps+1));
visited[r+1][c-2] = true;
}
if (isSafe(r-1,c+2,visited))
{
queue.add(new Cell(r-1,c+2,steps+1));
visited[r-1][c+2] = true;
}
if (isSafe(r+2,c-1,visited))
{
queue.add(new Cell(r+2,c-1,steps+1));
visited[r+2][c-1] = true;
}
if (isSafe(r-1,c-2,visited))
{
queue.add(new Cell(r-1,c-2,steps+1));
visited[r-1][c-2] = true;
}
if (isSafe(r-2,c-1,visited))
{
queue.add(new Cell(r-2,c-1,steps+1));
visited[r-2][c-1] = true;
}
}
}
//Function to find out minimum steps Knight needs to reach target position.
public int minStepToReachTarget(int KnightPos[], int TargetPos[], int N)
{
// Code here
result = false;
minSteps = Integer.MAX_VALUE;
n = N;
int i = 0, j = 0;
destRow = TargetPos[0]-1;
destCol = TargetPos[1]-1;
boolean[][] visited = new boolean[n][n];
checkReachability(KnightPos[0]-1, KnightPos[1]-1, visited);
return result ? minSteps : -1;
}
}