fork(3) download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long int lli;
  5.  
  6. #define all(arr) arr.begin(),arr.end()
  7. #define f first
  8. #define s second
  9. #define debug1(x) cout<<x<<"\n"
  10. #define debug2(x,y) cout<<x<<" "<<y<<"\n"
  11. #define debug3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
  12. #define nl cout<<"\n";
  13. #define pq priority_queue
  14. #define inf 0x3f3f3f3f
  15. #define test cout<<"abcd\n";
  16. #define pi pair<int,int>
  17. #define pii pair<int,pi>
  18. #define pb push_back
  19. #define MOD 1000000007
  20.  
  21. template <typename T>
  22. void input(vector<T> &arr,lli n) {
  23. T temp;
  24. for(lli i=0;i<n;i++) cin>>temp, arr.push_back(temp);
  25. }
  26.  
  27. template <typename T>
  28. void output(vector<T> arr) {
  29. for(auto x:arr) cout<<x<<" ";
  30. cout<<endl;
  31. }
  32.  
  33.  
  34. template <typename T>
  35. void input_set(set<T> &arr,lli n) {
  36. T temp;
  37. for(lli i=0;i<n;i++) cin>>temp, arr.insert(temp);
  38. }
  39.  
  40. lli mul(lli a, lli b) {
  41. return (a%MOD*b%MOD)%MOD;
  42. }
  43.  
  44. lli power(lli a,lli b) {
  45. lli ans = 1;
  46. while(b > 0) {
  47. if(b&1)
  48. ans = mul(ans, a);
  49. a = mul(a,a);;
  50. b >>= 1;
  51. }
  52. return ans;
  53. }
  54.  
  55. struct point {
  56. int r, c, moves;
  57. };
  58.  
  59. bool valid(int r, int c) {
  60. if(r>=1 and r<=8 and c>=1 and c<=8)
  61. return true;
  62. return false;
  63. }
  64.  
  65. void solve() {
  66. int r1, c1, r2, c2, rook_moves = 0, bishop_moves = 0, king_moves = 0;
  67. cin >> r1 >> c1 >> r2 >> c2;
  68. // For rook
  69. if(abs(r1-r2)!=0)
  70. ++rook_moves;
  71.  
  72. if(abs(c1-c2)!=0)
  73. ++rook_moves;
  74.  
  75. // BFS for bishop
  76. queue<point> q;
  77. q.push({r1, c1, 0});
  78. bool visited[9][9];
  79. memset(visited, false, sizeof(visited));
  80. while(q.size() != 0) {
  81. point top = q.front();
  82. q.pop();
  83. // move the whole diagonal from this point
  84. int r = top.r;
  85. int c = top.c;
  86. int moves = top.moves;
  87. // Checking if we have reached point r2,c2
  88. if(r==r2 and c==c2) {
  89. bishop_moves = moves;
  90. break;
  91. }
  92. // traverse diagonal
  93. for(int i=1; i<=7; i++) {
  94. if(valid(r-i, c-i) and visited[r-i][c-i] == false) {
  95. visited[r-i][c-i] = true;
  96. q.push({r-i, c-i, moves+1});
  97. }
  98.  
  99. if(valid(r-i, c+i) and visited[r-i][c+i] == false) {
  100. visited[r-i][c+i] = true;
  101. q.push({r-i, c+i, moves+1});
  102. }
  103.  
  104. if(valid(r+i, c-i) and visited[r+i][c-i] == false) {
  105. visited[r+i][c-i] = true;
  106. q.push({r+i, c-i, moves+1});
  107. }
  108.  
  109. if(valid(r+i, c+i) and visited[r+i][c+i] == false) {
  110. visited[r+i][c+i] = true;
  111. q.push({r+i, c+i, moves+1});
  112. }
  113. }
  114. }
  115.  
  116. // For king
  117. r1 = abs(r1-r2), c1 = abs(c1-c2);
  118. king_moves = max(r1, c1);
  119.  
  120. cout << rook_moves << " " << bishop_moves << " " << king_moves << endl;
  121. }
  122.  
  123. int main() {
  124.  
  125. ios_base::sync_with_stdio(false);
  126. cin.tie(NULL);
  127.  
  128. lli testcases = 1;
  129. while(testcases--) {
  130. solve();
  131. }
  132. }
  133.  
  134.  
Success #stdin #stdout 0s 4556KB
stdin
1 1 8 8
stdout
2 1 7