-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmin_steps_to_reach_destination_knight.cpp
77 lines (57 loc) · 1.74 KB
/
min_steps_to_reach_destination_knight.cpp
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
// { Driver Code Starts
// Initial function template for C++
#include <bits/stdc++.h>
using namespace std;
int minStepToReachTarget(int *, int *, int);
// Driver code to test above methods
int main() {
// size of square board
int t;
cin >> t;
while (t--) {
int N;
int a, b, c, d;
cin >> N;
cin >> a >> b;
cin >> c >> d;
int knightPos[] = {a, b};
int targetPos[] = {c, d};
cout << minStepToReachTarget(knightPos, targetPos, N) << endl;
}
return 0;
}
// } Driver Code Ends
// User function template for C++
// KnightPos : knight position coordinates
// targetPos : target position coordinated
// N : square matrix size
class Cell {
public:
int x, y, distance;
Cell(int a, int b, int dist) : x(a), y(b), distance(dist) {}
};
bool isValid(int &x, int &y, int N) {
return x>=1 and y>=1 and x<=N and y<=N;
}
int minStepToReachTarget(int knightPos[], int targetPos[], int N) {
// code here
vector<int> dx = {-2, -2, 2, 2, -1, -1, 1, 1};
vector<int> dy = {1, -1, 1, -1, 2, -2, 2, -2};
vector<vector<int>> visited(N+1, vector<int>(N+1, 0));
queue<Cell> Q;
Q.push(Cell(knightPos[0], knightPos[1], 0));
while(!Q.empty()) {
auto current = Q.front();
Q.pop();
if(current.x == targetPos[0] and current.y == targetPos[1]) return current.distance;
for(int i=0; i<8; i++) {
auto x = current.x + dx[i];
auto y = current.y + dy[i];
if(isValid(x, y, N) and !visited[x][y]) {
visited[x][y] = true;
Q.push(Cell(x, y, current.distance + 1));
}
}
}
return -1;
}