Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- bool isValid(int r, int c, int n)
- {
- if(r<1 || r>n || c<1 || c>n)return 0;
- return 1;
- }
- int main()
- {
- ios::sync_with_stdio(0);
- int n,x1,y1,x2,y2;
- cin>>n>>x1>>y1>>x2>>y2;
- int dx[]={-1,-2,1,2,-1,-2,1,2};
- int dy[]={-2,-1,2,1,2,1,-2,-1};
- bool visited[21][21]={};
- int ats[21][21]={};
- visited[x1][y1]=1;
- queue<int>r({x1}),c({y1});
- while(!r.empty())
- {
- int row = r.front();
- int col = c.front();
- r.pop();
- c.pop();
- for(int k=0; k<8; k++)
- if(isValid(row+dx[k],col+dy[k],n) && !visited[row+dx[k]][col+dy[k]])
- visited[row+dx[k]][col+dy[k]]=1,r.push(row+dx[k]),c.push(col+dy[k]),ats[row+dx[k]][col+dy[k]] = ats[row][col]+1;
- }
- cout<<ats[x2][y2]<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement