forked from Thelalitagarwal/GFG_Daily_Problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight Walk.cpp
More file actions
40 lines (38 loc) · 1.08 KB
/
Copy pathKnight Walk.cpp
File metadata and controls
40 lines (38 loc) · 1.08 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
class Solution {
public:
vector<vector<int>>m={ {2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2}};
bool isValid(int x, int y, int N)
{
if(x>0 && y>0 && x<=N && y<=N)
return true;
return false;
}
int minStepToReachTarget(vector<int>&KnightPos, vector<int>&TargetPos, int N){
queue<vector<int>>q;
q.push({KnightPos[0], KnightPos[1]});
bool vis[N+1][N+1]={false};
vis[KnightPos[0]][KnightPos[1]]=true;
int ans=0;
while(!q.empty())
{
int len=q.size();
for(int i=0; i<len; i++)
{
vector<int>temp=q.front();
q.pop();
if(temp[0]==TargetPos[0] && temp[1]==TargetPos[1])
return ans;
for(int j=0; j<m.size(); j++)
{
if(isValid(temp[0]+m[j][0],temp[1]+m[j][1],N) && !vis[temp[0]+m[j][0]][temp[1]+m[j][1]])
{
q.push({temp[0]+m[j][0],temp[1]+m[j][1]});
vis[temp[0]+m[j][0]][ temp[1]+m[j][1]]=1;
}
}
}
ans++;
}
return -1;
}
};