Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isValid(int r, int c, int n)
  5. {
  6. if(r<1 || r>n || c<1 || c>n)return 0;
  7. return 1;
  8. }
  9.  
  10. int main()
  11. {
  12. ios::sync_with_stdio(0);
  13. int n,x1,y1,x2,y2;
  14. cin>>n>>x1>>y1>>x2>>y2;
  15. int dx[]={-1,-2,1,2,-1,-2,1,2};
  16. int dy[]={-2,-1,2,1,2,1,-2,-1};
  17.  
  18. bool visited[21][21]={};
  19. int ats[21][21]={};
  20. visited[x1][y1]=1;
  21.  
  22. queue<int>r({x1}),c({y1});
  23.  
  24. while(!r.empty())
  25. {
  26. int row = r.front();
  27. int col = c.front();
  28. r.pop();
  29. c.pop();
  30.  
  31. for(int k=0; k<8; k++)
  32. if(isValid(row+dx[k],col+dy[k],n) && !visited[row+dx[k]][col+dy[k]])
  33. 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;
  34. }
  35.  
  36. cout<<ats[x2][y2]<<"\n";
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement