Count of squares reachable by a Bishop initially placed at top left on a given NxM chessboard

Given two integers N and M representing a N x M chessboard, the task is to find the maximum number of squares that the bishop can reach using any number of moves if initially it is placed in the top left corner of the chessboard.
Examples:
Input: N = 8, M = 8
Output: 32
Explanation: The bishop is initially standing on (1, 1) which is either a white or a black colour tile. Therefore, either all the black or the white tiles can be visited from (1, 1) using a sequence of moves depending on the color of the (1, 1) tile.ÂInput: N = 7, M = 3
Output: 11
Approach: The given problem can be solved by observing the fact that the number of reachable tiles from (1, 1) are the tiles with the same color as that of (1, 1). The count of such tiles can be calculated by the formula ceil((N*M)/2). The case where the above-mentioned statement proves wrong is the case where either N = 1 or M = 1. In such cases, no cells are reachable from (1, 1) and hence the required answer is 1.
Below is the implementation of the above approach:
C++
// C++ program of the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to find the maximum number of// reachable squares by a bishop in an// N x M chessboardint maximumSquares(int N, int M){Â Â Â Â // If either of N or M is equal to 1Â Â Â Â if (N == 1 || M == 1) {Â Â Â Â Â Â Â Â return 1;Â Â Â Â }Â
    // Otherwise the required    // answer is Ceil(N*M/2)    // Return Answer    return (N * M + 1) / 2;}Â
// Driver Codeint main(){Â Â Â Â int N = 7;Â Â Â Â int M = 3;Â
    cout << maximumSquares(N, M);Â
    return 0;} |
Java
// Java program for the above approachÂ
class GFG {Â
// Function to find the maximum number of// reachable squares by a bishop in an// N x M chessboardstatic int maximumSquares(int N, int M){Â Â Â Â Â Â Â // If either of N or M is equal to 1Â Â Â Â if (N == 1 || M == 1) {Â Â Â Â Â Â Â Â return 1;Â Â Â Â }Â
    // Otherwise the required    // answer is Ceil(N*M/2)    // Return Answer    return (N * M + 1) / 2;}Â
// Driver Codepublic static void main (String[] args) {Â Â Â Â Â Â Â Â Â Â Â Â Â int N = 7;Â Â Â Â int M = 3;Â
    System.out.println(maximumSquares(N, M));}}Â
// This code is contributed by target_2. |
Python
# Python program of the above approachÂ
# Function to find the maximum number of# reachable squares by a bishop in an# N x M chessboarddef maximumSquares(N, M) :Â
    # If either of N or M is equal to 1    if (N == 1 or M == 1) :        return 1Â
    # Otherwise the required    # answer is Ceil(N*M/2)    # Return Answer    return (N * M + 1) // 2Â
# Driver Codeif __name__ == "__main__" :Â Â Â Â N = 7Â Â Â Â M = 3Â
    print(maximumSquares(N, M))         # This code is contributed by Samim Hossain Mondal. |
C#
// C# program for the above approachusing System;Â
class GFG {Â
// Function to find the maximum number of// reachable squares by a bishop in an// N x M chessboardstatic int maximumSquares(int N, int M){Â Â Â Â Â Â Â // If either of N or M is equal to 1Â Â Â Â if (N == 1 || M == 1) {Â Â Â Â Â Â Â Â return 1;Â Â Â Â }Â
    // Otherwise the required    // answer is Ceil(N*M/2)    // Return Answer    return (N * M + 1) / 2;}Â
// Driver Codepublic static void Main () {Â Â Â Â Â Â Â Â Â Â Â Â Â int N = 7;Â Â Â Â int M = 3;Â
    Console.Write(maximumSquares(N, M));}}Â
// This code is contributed by Samim Hossain Mondal. |
Javascript
<script>// Javascript program of the above approachÂ
// Function to find the maximum number of// reachable squares by a bishop in an// N x M chessboardfunction maximumSquares(N, M){Â Â Â Â // If either of N or M is equal to 1Â Â Â Â if (N == 1 || M == 1) {Â Â Â Â Â Â Â Â return 1;Â Â Â Â }Â
    // Otherwise the required    // answer is Ceil(N*M/2)    // Return Answer    return (N * M + 1) / 2;}Â
// Driver Codelet N = 7;let M = 3;Â
document.write(maximumSquares(N, M));Â
// This code is contributed by Samim Hossain Mondal.</script> |
Â
Â
11
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



