
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum bishops that can be placed on N*N chessboard in C++
We are given an input N which denotes the size of the chessboard. The task here is to find for any value of N, how many bishops can be placed on the NXN chessboard such that no two bishops can attack each other. Let’s understand with examples.
Input − N=2
Output− Maximum bishops that can be placed on N*N chessboard − 2 ( as shown above )
Explanation − As depicted above the only non-contradictory positions are where the bishops are placed. Bishops at-most for 2X2 chessboard.
Input − N=5
Output− Maximum bishops that can be placed on N*N chessboard: 8 ( as shown above )
Approach used in the below program is as follows
We take an integer value N as input for chessboard dimensions.
Pass this N as argument to totalBishops(int n).
For N<1 invalid input, bishops count=0.
For N=1, only 1 position, bishop count=1.
Else bishops will be 2*(N-1)
Store this result in variable bishops.
Return the result.
Example
#include <iostream> //to return maximum bishops possible int totalBishops(int n){ int bishops=0; if (n < 1) bishops= 0; else if (n == 1) bishops= 1; else bishops= 2 * (n - 1); return bishops; } int main(){ int N = 15; //for chessboard dimensions N*N printf("%d" ,totalBishops(N)); return 0; }
Output
If we run the above code it will generate the following output −
28
- Related Articles
- Maximum number that can be display on Seven Segment Display using N segments in C++
- Maximum litres of water that can be bought with N Rupees in C++
- maximum length of continuous silk thread that can be obtained from a cocoon."\n
- Prove that the $n^{th}$ term of an A.P. can't be $n^2+1$.
- Program to find maximum units that can be put on a truck in Python
- A spring balance can withstand a weight of \( 980 \mathrm{~N} \) on earth without getting deformed. What should be the maximum mass of a body that can be weighed using this spring balance on the moon?
- Find a positive number M such that gcd(N^M,N&M) is maximum in Python
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of candies that can be bought in C
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts in Python
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts in C++
- Maximum money that can be withdrawn in two steps in C
- Maximum Average Subtree in Python\n
- What is the maximum number of electrons that can go into the N shell of an atom?
- Check if a king can move a valid move or not when N nights are there in a modified chessboard in Python
