Given the position of a **Bishop** on an **8 * 8 chessboard**, the task is to count the total number of squares that can be visited by the Bishop in one move. The position of the Bishop is denoted using row and column number of the chessboard.

**Examples:**

Input:Row = 4, Column = 4

Output:13

Input:Row = 1, Column = 1

Output:7

**Approach:** In the game of chess, a Bishop can only move diagonally and there is no restriction in distance for each move.

So, We can also say that Bishop can move in four ways i.e. diagonally top left, top right, bottom left and bottom right from current position.

We can calculate the numbers of squares visited in each move by:

Total squares visited in Top Left move= min(r, c) – 1

Total squares visited in Top Right move= min(r, 9 – c) – 1

Total squares visited in Bottom Left move= 8 – max(r, 9 – c)

Total squares visited in Bottom Right move= 8 – max(r, c)

where,randcare the coordinates of the current position of the Bishop on the chessboard.

Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// total positions the Bishop ` `// can visit in a single move ` `int` `countSquares(` `int` `row, ` `int` `column) ` `{ ` ` ` ` ` `// Count top left squares ` ` ` `int` `topLeft = min(row, column) - 1; ` ` ` ` ` `// Count bottom right squares ` ` ` `int` `bottomRight = 8 - max(row, column); ` ` ` ` ` `// Count top right squares ` ` ` `int` `topRight = min(row, 9 - column) - 1; ` ` ` ` ` `// Count bottom left squares ` ` ` `int` `bottomLeft = 8 - max(row, 9 - column); ` ` ` ` ` `// Return total count ` ` ` `return` `(topLeft + topRight + bottomRight + bottomLeft); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `// Bishop's Position ` ` ` `int` `row = 4, column = 4; ` ` ` ` ` `cout << countSquares(row, column); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `class` `GFG { ` ` ` ` ` `// Function to return the count of ` ` ` `// total positions the Bishop ` ` ` `// can visit in a single move ` ` ` `static` `int` `countSquares(` `int` `row, ` `int` `column) ` ` ` `{ ` ` ` ` ` `// Count top left squares ` ` ` `int` `topLeft = Math.min(row, column) - ` `1` `; ` ` ` ` ` `// Count bottom right squares ` ` ` `int` `bottomRight = ` `8` `- Math.max(row, column); ` ` ` ` ` `// Count top right squares ` ` ` `int` `topRight = Math.min(row, ` `9` `- column) - ` `1` `; ` ` ` ` ` `// Count bottom left squares ` ` ` `int` `bottomLeft = ` `8` `- Math.max(row, ` `9` `- column); ` ` ` ` ` `// Return total count ` ` ` `return` `(topLeft + topRight + bottomRight + bottomLeft); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `// Bishop's Position ` ` ` `int` `row = ` `4` `, column = ` `4` `; ` ` ` ` ` `System.out.println(countSquares(row, column)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function to return the count of ` ` ` `// total positions the Bishop ` ` ` `// can visit in a single move ` ` ` `static` `int` `countSquares(` `int` `row, ` `int` `column) ` ` ` `{ ` ` ` ` ` `// Count top left squares ` ` ` `int` `topLeft = Math.Min(row, column) - 1; ` ` ` ` ` `// Count bottom right squares ` ` ` `int` `bottomRight = 8 - Math.Max(row, column); ` ` ` ` ` `// Count top right squares ` ` ` `int` `topRight = Math.Min(row, 9 - column) - 1; ` ` ` ` ` `// Count bottom left squares ` ` ` `int` `bottomLeft = 8 - Math.Max(row, 9 - column); ` ` ` ` ` `// Return total count ` ` ` `return` `(topLeft + topRight + bottomRight + bottomLeft); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `// Bishop's Position ` ` ` `int` `row = 4, column = 4; ` ` ` ` ` `Console.WriteLine(countSquares(row, column)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` ` ` `# Function to return the count of ` `# total positions the Bishop ` `# can visit in a single move ` `def` `countSquares(row, column): ` ` ` ` ` `# Count top left squares ` ` ` `topLeft ` `=` `min` `(row, column) ` `-` `1` ` ` ` ` `# Count bottom right squares ` ` ` `bottomRight ` `=` `8` `-` `max` `(row, column) ` ` ` ` ` `# Count top right squares ` ` ` `topRight ` `=` `min` `(row, ` `9` `-` `column) ` `-` `1` ` ` ` ` `# Count bottom left squares ` ` ` `bottomLeft ` `=` `8` `-` `max` `(row, ` `9` `-` `column) ` ` ` ` ` ` ` `# Return total count ` ` ` `return` `(topLeft ` `+` `topRight ` `+` `bottomRight ` `+` `bottomLeft) ` ` ` `# Driver code ` ` ` `# Bishop's Position ` `row ` `=` `4` `column ` `=` `4` ` ` `print` `(countSquares(row, column)) ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Function to return the count of ` `// total positions the Bishop ` `// can visit in a single move ` `function` `countSquares(` `$row` `, ` `$column` `) ` `{ ` ` ` ` ` `// Count top left squares ` ` ` `$topLeft` `= min(` `$row` `, ` `$column` `) - 1; ` ` ` ` ` `// Count bottom right squares ` ` ` `$bottomRight` `= 8 - max(` `$row` `, ` `$column` `); ` ` ` ` ` `// Count top right squares ` ` ` `$topRight` `= min(` `$row` `, 9 - ` `$column` `) - 1; ` ` ` ` ` `// Count bottom left squares ` ` ` `$bottomLeft` `= 8 - max(` `$row` `, 9 - ` `$column` `); ` ` ` ` ` `// Return total count ` ` ` `return` `(` `$topLeft` `+ ` `$topRight` `+ ` ` ` `$bottomRight` `+ ` `$bottomLeft` `); ` `} ` ` ` `// Driver code ` ` ` `// Bishop's Position ` `$row` `= 4; ` `$column` `= 4; ` `echo` `countSquares(` `$row` `, ` `$column` `); ` ` ` `// This code is contributed by jit_t ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

13

## Recommended Posts:

- Count positions in a chessboard that can be visited by the Queen which are not visited by the King
- Check whether Bishop can take down Pawn or not
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Check if a cell can be visited more than once in a String
- Count distinct points visited on the number line
- Printing pre and post visited times in DFS of a graph
- Find cells in Matrix that are not visited by Robot for given movements
- Total ways of choosing X men and Y women from a total of M men and W women
- Number of cells a queen can move with obstacles on the chessborad
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Count of squares that can be drawn without lifting the pencil
- Count elements in an Array that can be represented as difference of two perfect squares
- Count of primes in a given range that can be expressed as sum of perfect squares
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Total number of days taken to complete the task if after certain days one person leaves
- Check whether a number can be represented by sum of two squares
- Check whether a number can be represented as difference of two squares
- Check whether a number can be represented by the product of two squares
- Total number of different staircase that can made from N boxes

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.