Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GFG Knight Walk Problem (Class 23) https://practice.geeksforgeeks.org/problems/knight-walk4521/1
- struct box{
- int x;
- int y;
- int step;
- };
- class Solution {
- public:
- int minStepToReachTarget(vector<int>&K, vector<int>&T, int N){
- // Code here
- vector<vector<bool>>v(N+1,vector<bool>(N+1,false));
- queue<box> q;
- int xpos[8] = {2,2,-2,-2,1,1,-1,-1};
- int ypos[8] = {1,-1,1,-1,2,-2,2,-2};
- q.push({K[0],K[1],0});
- while(q.size()!=0){
- int x = q.front().x;
- int y = q.front().y;
- int step = q.front().step;
- q.pop();
- if(x==T[0] && y==T[1])return step;
- for(int i =0; i<8; i++){
- int xnew = x+xpos[i];
- int ynew = y+ypos[i];
- if(xnew>=1 && ynew>=1 && xnew<=N && ynew<=N && v[xnew][ynew]==false){
- q.push({xnew,ynew,step+1});
- v[xnew][ynew] = true;
- }
- }
- }
- return 0;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement