Given an integer N and bishop place {x, y}, the duty is to print all attainable units of factors that the bishop can attain in one transfer on an N*N chessboard.Â
Notice: Output the factors in sorted order.
Examples: Â
Enter: n = 2, x = 0, y = 0
Output: (0, 0), (1, 1)Enter: n = 3, x = 1, y=1
Output: (0, 0), (0, 2), (1, 1) (2, 0), (2, 2)
Naive Method: This may be solved with the next thought:
A bishop can solely journey diagonally on a chessboard. Take into account the diagonal factors from the given level (i, j).
Under are the steps concerned within the implementation of the code:
- Make vector pairs with the intention to retailer the factors.
- Add the present place of the bishop within the vector.
- Test for the factors that may be reached close to the given level.
- Be sure that to not transfer outdoors the boundaries of the chessboard.
- Now kind the factors in lexicographical order.
- Test for duplicate factors and eventually print the factors.
Under is the implementation of the above method:Â
C++
// C++ Implementation of the above method #embrace <bits/stdc++.h> utilizing namespace std; // Perform to print all attainable positions // of bishop void positionofbishop(int n, int x, int y) { vector<pair<int, int> > factors; // Add present place factors.push_back({ x, y }); // Calculate attainable factors // in high left path for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) { factors.push_back({ i, j }); } // Calculate attainable factors // in high proper path for (int i = x - 1, j = y + 1; i >= 0 && j < n; i--, j++) { factors.push_back({ i, j }); } // Calculate attainable factors // in backside left path for (int i = x + 1, j = y - 1; i < n && j >= 0; i++, j--) { factors.push_back({ i, j }); } // Calculate attainable factors // in backside proper path for (int i = x + 1, j = y + 1; i < n && j < n; i++, j++) { factors.push_back({ i, j }); } // Cort the factors in // lexicographical order kind(factors.start(), factors.finish()); // Take away duplicates auto final = distinctive(factors.start(), factors.finish()); factors.erase(final, factors.finish()); // Print all attainable factors for (auto p : factors) { cout << "(" << p.first << ", " << p.second << ")" << endl; } } // Driver code int important() { int n = 3, x = 1, y = 1; // Perform name positionofbishop(n, x, y); return 0; }
(0, 0) (0, 2) (1, 1) (2, 0) (2, 2)
Time Complexity: O(n2)
Auxiliary Area: O(n)
Optimum Method:Â
The concept is to traverse the board in 4 instructions (top-left, top-right, bottom-left, bottom-right) and cease once we hit the sting of the board or a diagonal that intersects with the bishop’s present diagonal. By doing so, we will assure that we solely go to every sq. as soon as and keep away from sorting the factors or utilizing further reminiscence to retailer them.
Under is the implementation of the above method:Â
C++
// C++ code for the above method: #embrace <bits/stdc++.h> utilizing namespace std; // Perform to print all attainable factors // the place bishop can attain void positionofbishop(int n, int x, int y) { vector<pair<int, int> > factors; // Add present place factors.push_back({ x, y }); // Calculate variety of squares // within the top-left diagonal int tl = min(x, y); // Calculate variety of squares // within the top-right diagonal int tr = min(x, n - 1 - y); // Calculate variety of squares // within the bottom-left diagonal int bl = min(n - 1 - x, y); // Calculate variety of squares // within the bottom-right diagonal int br = min(n - 1 - x, n - 1 - y); // Add all attainable factors for (int i = 1; i <= tl; i++) { factors.push_back({ x - i, y - i }); } for (int i = 1; i <= tr; i++) { factors.push_back({ x - i, y + i }); } for (int i = 1; i <= bl; i++) { factors.push_back({ x + i, y - i }); } for (int i = 1; i <= br; i++) { factors.push_back({ x + i, y + i }); } // Print all attainable factors // in sorted order kind(factors.start(), factors.finish()); for (auto p : factors) { cout << "(" << p.first << ", " << p.second << ")" << endl; } } // Driver code int important() { int n = 3, x = 1, y = 1; // Perform name positionofbishop(n, x, y); return 0; }
(0, 0) (0, 2) (1, 1) (2, 0) (2, 2)
Time Complexity: O(nlogn)
Auxiliary Area: O(1)