- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

On a chessboard represented as 8 X 8 grid we are given the position of Bishop in form of row and column position. The goal is to find the total number of squares that Bishop can visit in one move. We know the Bishop can move in all directions (diagonally left up/down and right up/down).

**For Example**

row = 5, column = 4

Count of total number of squares that can be visited by Bishop in one move are: 13

As shown in above figure the squares that Bishop can cover are 9.

row = 1, column = 1

Count of total number of squares that can be visited by Bishop in one move are: 7

As this is the corner most position, then Bishop can only cover one diagonal which can have a maximum of 7 squares.

**Approach used in the below program is as follows** −

In this approach we will calculate the diagonal squares using horizontal and vertical maximum and minimum square position.

Take integers row and column for position of bishop.

Function squares_visited(int first, int second) takes the position of Bishop and returns the count of squares that it can visit in one move.

Take the initial count as 0.

The minimum of the left position is the minimum of row or column position −1.

The maximum of the left position is 8 − maximum of row or 9−column position.

The minimum of the right position is the minimum of row or 9−column position −1.

The maximum of the right position is 8 − maximum of row or column position.

Total squares will be the sum of above calculated positions.

Return count as result.

#include <bits/stdc++.h> using namespace std; int squares_visited(int first, int second){ int count = 0; int min_left = min(first, second) − 1; int max_left = 8 − max(first, 9 − second); int max_right = 8 − max(first, second); int min_right = min(first, 9 − second) − 1; count = min_left + min_right + max_right + max_left; return count; } int main(){ int row = 3, column = 3; cout<<"Count of total number of squares that can be visited by Bishop in one move are: "<<squares_visited(row, column); return 0; }

If we run the above code it will generate the following output −

Count of total number of squares that can be visited by Bishop in one move are: 11

- Related Questions & Answers
- Count distinct points visited on the number line in C++
- Count number of squares in a rectangle in C++
- Count Triplets such that one of the numbers can be written as sum of the other two in C++
- Count unique numbers that can be generated from N by adding one and removing trailing zeros in C++
- Count the total number of elements in the List in C#?
- Count all possible position that can be reached by Modified Knight in C++
- Maximum number of 2×2 squares that can be fit inside a right isosceles triangle in C
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of candies that can be bought in C
- Minimize the total number of teddies to be distributed in C++
- Program to find maximum number of package that can be bought by buyers in C++
- Maximum number of squares that can fit in a right angle isosceles triangle in C++
- Count total bits in a number in C++
- Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++
- How to count the total number of frames in OpenCV using C++?

Advertisements