Repository navigation
Expand file tree
/
Copy pathstepsByKnight.cpp
More file actions
58 lines (52 loc) · 1.6 KB
/
Copy pathstepsByKnight.cpp
File metadata and controls
58 lines (52 loc) · 1.6 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
// https://www.geeksforgeeks.org/problems/steps-by-knight5927/1?page=1&category=Graph&sortBy=submissions
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl '\n'
class Solution
{
int dx[8] = {1, -1, -2, -2, -1, 1, 2, 2};
int dy[8] = {-2, -2, -1, 1, 2, 2, 1, -1};
public:
// Function to find out minimum steps Knight needs to reach target position.
int minStepToReachTarget(vector<int> &start, vector<int> &end, int N)
{
if (start == end)
return 0;
// x, y, moves
queue<pair<pair<int, int>, int>> q;
vector<vector<bool>> visited(N, vector<bool>(N, false));
q.push({{start[0] - 1, start[1] - 1}, 0});
visited[start[0] - 1][start[1] - 1] = true;
while (!q.empty())
{
// cout << "hello";
int x = q.front().first.first;
int y = q.front().first.second;
int move = q.front().second;
q.pop();
if (x == end[0] - 1 && y == end[1] - 1)
return move;
for (int i = 0; i < 8; i++)
{
int row = x + dx[i];
int col = y + dy[i];
if (row >= 0 && row < N && col >= 0 && col < N && visited[row][col] == false)
{
q.push({{row, col}, move + 1});
visited[row][col] = true;
}
}
}
return -1;
}
};
int main()
{
int N;
cin >> N;
vector<int> start = {1, 1};
vector<int> end = {N, N};
Solution obj;
cout << obj.minStepToReachTarget(start, end, N);
}