fork download
  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4.  
  5. bool isValidKnight1(int getLocX,int getLocY, int x,int y)
  6. {
  7. return (x == getLocX + 1 && y == getLocY + 2
  8. || x == getLocX - 1 && y == getLocY + 2
  9. || y == getLocY + 1 && x == getLocX + 2
  10. || y == getLocY + 1 && x == getLocX - 2
  11. || x == getLocX - 1 && y == getLocY + 2
  12. || x == getLocX + 1 && y == getLocY - 2
  13. || y == getLocY - 1 && x == getLocX - 2
  14. || y == getLocY - 1 && x == getLocX + 2
  15. || x == getLocX - 1 && y == getLocY - 2);
  16. }
  17. bool isValidKnight2(int getLocX,int getLocY, int x,int y)
  18. {
  19. int dx = abs(getLocX - x);
  20. int dy = abs(getLocY - y);
  21. return (dx == 1 && dy==2 || dx == 2 && dy==1);
  22. }
  23. bool isValidKnight3(int getLocX,int getLocY, int x,int y)
  24. {
  25. static set<pair<int,int>> valid={{1,2},{2,1}};
  26. return (valid.find(make_pair(abs(getLocX - x), abs(getLocY - y)))!=valid.end());
  27. }
  28. bool isValidKnight4(int getLocX,int getLocY, int x,int y)
  29. {
  30. int d= abs(getLocX - x)<<4 | abs(getLocY - y);
  31. return (d==0x21||d==0x12);
  32. }
  33.  
  34. int main() {
  35. int cx,cy,fx,fy;
  36. while (cin>>cx>>cy>>fx>>fy) {
  37. cout << cx<<","<<cy<<" -> "<<fx<<","<<fy<<": ";
  38. cout << isValidKnight1(cx,cy,fx,fy);
  39. cout << isValidKnight2(cx,cy,fx,fy);
  40. cout << isValidKnight3(cx,cy,fx,fy);
  41. cout << isValidKnight4(cx,cy,fx,fy);
  42. cout <<endl;
  43. }
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 15240KB
stdin
1 1 2 3
1 1 3 2 
3 2 1 1 
2 3 1 1 
1 1 5 1
1 1 5 4
5 5 5 5 
5 5 6 7 
5 5 7 6
5 5 4 3
5 5 3 4
5 5 4 6 
5 5 6 4
5 5 7 4
5 5 4 7 
5 5 3 6
5 5 6 3
stdout
1,1 -> 2,3: 1111
1,1 -> 3,2: 1111
3,2 -> 1,1: 1111
2,3 -> 1,1: 1111
1,1 -> 5,1: 0000
1,1 -> 5,4: 0000
5,5 -> 5,5: 0000
5,5 -> 6,7: 1111
5,5 -> 7,6: 1111
5,5 -> 4,3: 1111
5,5 -> 3,4: 1111
5,5 -> 4,6: 0000
5,5 -> 6,4: 0000
5,5 -> 7,4: 1111
5,5 -> 4,7: 1111
5,5 -> 3,6: 1111
5,5 -> 6,3: 1111