 Open in App
Not now

# Maximum cells attacked by Rook or Bishop in given Chessboard

• Last Updated : 31 May, 2022

Given three integers N, R, and C representing an N*N chessboard and the position (R, C) where the rook and the bishop is placed. The task is to find out who can attack the most number of cells (except the cell they are in) and how many.

Note:

• A rook can move only horizontally along the row or vertically along the column and any number of cells at a time
• A bishop can move diagonally any number of cells at a time.

Examples:

Input: N = 3, R = 2, C = 1
Output: Rook, 4
Explanation: Rook can attack 2 cells in the row and 2 cells along the column. So 2+2 = 4.
Bishop can attack only 2 cells (1, 2) and (3, 2).

Input: N=1, R=1, C=1
Output: Both, 0

Approach: The problem can be solved by the following observation:

A rook can move vertically upwards or downwards and horizontally to the left or to the right.
So total cells attacked by rook = (N – 1) + (N – 1) = 2*(N – 1)

A bishop can attack only diagonally i.e., across the primary diagonal or the secondary diagonal.
So total number of cells attacked along the main diagonal is min(R-1, C-1) + min(N-R, N-C).
Total number of cells attacked along the secondary diagonal is min(R-1, N-C) + min(N-R, C-1)

Follow the steps below to solve the problem:

• Find the total cells under attack by the rook (say X) using the above formula.
• Find the total cells under attack by the bishop (say Y) using the above formula.
• Check which one is greater and return the answer accordingly.

Below is the implementation of the above approach:

## C++

 `// C++ code to implement the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find No of cells Elephant` `// and Camel can attack and return max of that` `pair<``int``, ``int``> fun(``int` `n, ``int` `r, ``int` `c)` `{`   `    ``// Edge Case` `    ``if` `(n == 1)` `        ``return` `make_pair(0, 0);`   `    ``// For Rook` `    ``int` `row = n - 1, col = n - 1;`   `    ``// For Bishop` `    ``int` `UL = min(r - 1, c - 1);` `    ``int` `UR = min(r - 1, n - c);` `    ``int` `DL = min(n - r, c - 1);` `    ``int` `DR = min(n - r, n - c);`   `    ``// Count total moves of Rook` `    ``int` `E = row + col;`   `    ``// Count total moves of Bishop` `    ``int` `C = DL + DR + UL + UR;`   `    ``// Return maximum among two, consider` `    ``// 0 for Rook, 1 for Bishop, -1 for both` `    ``if` `(E == C)` `        ``return` `{ -1, E };` `    ``if` `(E > C)` `        ``return` `{ 0, E };` `    ``return` `{ 1, C };` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 3, R = 2, C = 1;`   `    ``// Function call` `    ``pair<``int``, ``int``> p = fun(N, R, C);`   `    ``if` `(p.first == -1)` `        ``cout << ``"Both, "``;` `    ``else` `if` `(p.first == 0)` `        ``cout << ``"Rook, "``;` `    ``else` `        ``cout << ``"Bishop, "``;` `    ``cout << p.second << endl;` `    ``return` `0;` `}`

## Java

 `/*package whatever //do not write package name here */`   `import` `java.io.*;`   `class` `GFG {` `  ``// Function to find No of cells Elephant` `  ``// and Camel can attack and return max of that` `  ``static` `int``[] fun(``int` `n, ``int` `r, ``int` `c)` `  ``{`   `    ``int``[] res = ``new` `int``[``2``];` `    ``// Edge Case` `    ``if` `(n == ``1``){` `      ``res[``0``] = ``0``;` `      ``res[``1``] = ``0``;` `      ``return` `res;` `    ``}`   `    ``// For Rook` `    ``int` `row = n - ``1``, col = n - ``1``;`   `    ``// For Bishop` `    ``int` `UL = Math.min(r - ``1``, c - ``1``);` `    ``int` `UR = Math.min(r - ``1``, n - c);` `    ``int` `DL = Math.min(n - r, c - ``1``);` `    ``int` `DR = Math.min(n - r, n - c);`   `    ``// Count total moves of Rook` `    ``int` `E = row + col;`   `    ``// Count total moves of Bishop` `    ``int` `C = DL + DR + UL + UR;`   `    ``// Return maximum among two, consider` `    ``// 0 for Rook, 1 for Bishop, -1 for both` `    ``if` `(E == C){` `      ``res[``0``] = -``1``;` `      ``res[``1``] = E;` `    ``}` `    ``else` `if` `(E > C){` `      ``res[``0``] = ``0``;` `      ``res[``1``] = E;` `    ``}` `    ``else``{` `      ``res[``0``] = ``1``;` `      ``res[``1``] = C;` `    ``}` `    ``return` `res;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main(String args[])` `  ``{` `    ``int` `N = ``3``, R = ``2``, C = ``1``;`   `    ``// Function call` `    ``int``[] p = fun(N, R, C);`   `    ``if` `(p[``0``] == -``1``)` `      ``System.out.print(``"Both, "``);` `    ``else` `if` `(p[``0``] == ``0``)` `      ``System.out.print(``"Rook, "``);` `    ``else` `      ``System.out.print(``"Bishop, "``);` `    ``System.out.println(p[``1``]);` `  ``}` `}`   `// This code is contributed by shinjanpatra`

## Python3

 `# Python program for the above approach`   `# Function to find No of cells Elephant` `# and Camel can attack and return max of that` `def` `fun(n, r, c):`   `    ``# Edge Case` `    ``if` `(n ``=``=` `1``):` `        ``return` `[``0``, ``0``];`   `    ``# For Rook` `    ``row ``=` `n ``-` `1` `    ``col ``=` `n ``-` `1`   `    ``# For Bishop` `    ``UL ``=` `min``(r ``-` `1``, c ``-` `1``);` `    ``UR ``=` `min``(r ``-` `1``, n ``-` `c);` `    ``DL ``=` `min``(n ``-` `r, c ``-` `1``);` `    ``DR ``=` `min``(n ``-` `r, n ``-` `c);`   `    ``# Count total moves of Rook` `    ``E ``=` `row ``+` `col;`   `    ``# Count total moves of Bishop` `    ``C ``=` `DL ``+` `DR ``+` `UL ``+` `UR;`   `    ``# Return maximum among two, consider` `    ``# 0 for Rook, 1 for Bishop, -1 for both` `    ``if` `(E ``=``=` `C):` `        ``return` `[``-``1``, E];` `    ``if` `(E > C):` `        ``return` `[``0``, E];` `    ``return` `[``1``, C];`     `# Driver Code`   `N ``=` `3` `R ``=` `2` `C ``=` `1`   `# Function call` `p ``=` `fun(N, R, C);`   `if` `(p[``0``] ``=``=` `-``1``):` `    ``print``(``"Both, "``, end``=``"");` `elif` `(p[``0``] ``=``=` `0``):` `    ``print``(``"Rook, "``, end``=``"");` `else``:` `    ``print``(``"Bishop, "``, end``=``"");` `print``(p[``1``]);`   `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# program for the above approach` `using` `System;`   `public` `class` `GFG ` `{` `  `  `  ``// Function to find No of cells Elephant` `  ``// and Camel can attack and return max of that` `  ``static` `int``[] fun(``int` `n, ``int` `r, ``int` `c)` `  ``{`   `    ``int``[] res = ``new` `int``;` `    `  `    ``// Edge Case` `    ``if` `(n == 1){` `      ``res = 0;` `      ``res = 0;` `      ``return` `res;` `    ``}`   `    ``// For Rook` `    ``int` `row = n - 1, col = n - 1;`   `    ``// For Bishop` `    ``int` `UL = Math.Min(r - 1, c - 1);` `    ``int` `UR = Math.Min(r - 1, n - c);` `    ``int` `DL = Math.Min(n - r, c - 1);` `    ``int` `DR = Math.Min(n - r, n - c);`   `    ``// Count total moves of Rook` `    ``int` `E = row + col;`   `    ``// Count total moves of Bishop` `    ``int` `C = DL + DR + UL + UR;`   `    ``// Return maximum among two, consider` `    ``// 0 for Rook, 1 for Bishop, -1 for both` `    ``if` `(E == C){` `      ``res = -1;` `      ``res = E;` `    ``}` `    ``else` `if` `(E > C){` `      ``res = 0;` `      ``res = E;` `    ``}` `    ``else``{` `      ``res = 1;` `      ``res = C;` `    ``}` `    ``return` `res;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main(String []args)` `  ``{` `    ``int` `N = 3, R = 2, C = 1;`   `    ``// Function call` `    ``int``[] p = fun(N, R, C);`   `    ``if` `(p == -1)` `      ``Console.Write(``"Both, "``);` `    ``else` `if` `(p == 0)` `      ``Console.Write(``"Rook, "``);` `    ``else` `      ``Console.Write(``"Bishop, "``);` `      `  `    ``Console.WriteLine(p);` `  ``}` `}`   `// This code is contributed by AnkThon`

## Javascript

 ``

Output

`Rook, 4`

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles