Given an integer N and bishop place {x, y}, the duty is to print all potential 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 Strategy: 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).
Beneath are the steps concerned within the implementation of the code:
- Make vector pairs as a way to retailer the factors.
- Add the present place of the bishop within the vector.
- Examine for the factors that may be reached close to the given level.
- Ensure that to not transfer exterior the boundaries of the chessboard.
- Now kind the factors in lexicographical order.
- Examine for duplicate factors and eventually print the factors.
Beneath is the implementation of the above strategy:
C++
// C++ Implementation of the above strategy
#embrace <bits/stdc++.h>
utilizing namespace std;
// Operate to print all potential 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 potential factors
// in prime left route
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0;
i--, j--) {
factors.push_back({ i, j });
}
// Calculate potential factors
// in prime proper route
for (int i = x - 1, j = y + 1; i >= 0 && j < n;
i--, j++) {
factors.push_back({ i, j });
}
// Calculate potential factors
// in backside left route
for (int i = x + 1, j = y - 1; i < n && j >= 0;
i++, j--) {
factors.push_back({ i, j });
}
// Calculate potential factors
// in backside proper route
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 potential factors
for (auto p : factors) {
cout << "(" << p.first << ", " << p.second << ")"
<< endl;
}
}
// Driver code
int important()
{
int n = 3, x = 1, y = 1;
// Operate 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 Strategy:
The thought 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 are able to assure that we solely go to every sq. as soon as and keep away from sorting the factors or utilizing additional reminiscence to retailer them.
Beneath is the implementation of the above strategy:
C++
// C++ code for the above strategy:
#embrace <bits/stdc++.h>
utilizing namespace std;
// Operate to print all potential 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 potential 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 potential 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;
// Operate 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)