Count the total number of squares that can be visited by Bishop in one move

23 Jun, 2022
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: 13Input: 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, r and c are the coordinates of the current position of the Bishop on the chessboard.  
Below is the implementation of the above approach: 
C++
Java
C#
Python3
PHP
Javascript
// 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 moveint 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 codeint main(){     // Bishop's Position    int row = 4, column = 4;     cout << countSquares(row, column);     return 0;}
// Java implementation of above approachclass 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));    }}
// C# implementation of above approachusing 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));    }}
# Python3 implementation of above approach # Function to return the count of# total positions the Bishop# can visit in a single movedef 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 Positionrow = 4column = 4 print(countSquares(row, column))
<?php// PHP implementation of above approach // Function to return the count of// total positions the Bishop// can visit in a single movefunction 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?>
<script> // Javascript implementation of above approach // Function to return the count of// total positions the Bishop// can visit in a single movefunction countSquares(row, column){     // Count top left squares    var topLeft = Math.min(row, column) - 1;     // Count bottom right squares    var bottomRight = 8 - Math.max(row, column);     // Count top right squares    var topRight = Math.min(row, 9 - column) - 1;     // Count bottom left squares    var bottomLeft = 8 - Math.max(row, 9 - column);     // Return total count    return (topLeft + topRight + bottomRight + bottomLeft);} // Driver code// Bishop's Positionvar row = 4, column = 4;document.write( countSquares(row, column)); </script>
13
 
Time Complexity: O(1)
Auxiliary Space: O(1)
jit_t
rutvik_56
subhamkumarm348
chessboard-problems
Competitive Programming
Mathematical
Matrix
Mathematical
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modulo 10^9+7 (1000000007)
Prefix Sum Array - Implementation and Applications in Competitive Programming
Bits manipulation (Important tactics)
What is Competitive Programming and How to Prepare for It?
Count of strings whose prefix match with the given string to a given length k
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays