Given an 8 X 8 matrix of characters ‘*’ and ‘K’ as a chessboard in which K represents the position of the King and the rest of the cell is represented by *.
Also given the following three sets of pieces:
Set 1: 1 Queen
Set 2: 1 Queen, 1 Rook
Set 3: 1 Queen, 1 Rook, 1 Bishop
Then you have to trap the King in such a way that all conditions should be met:
- Trap the King by one of the given sets and you can’t use Set 2(Queen, Rook) if trapping is possible by Set 1(Queen), same with Set 3 and Set 2 respectively.
- King should not be able to make any of its possible moves.
- King should not be directly under attack by any piece of used Set to trap.
Then the task is to print an 8 X 8 matrix as output, in which ‘Q’ will represent Queen, ‘R’ will represent Rook, ‘B’ will represent ‘Bishop’, according to the set used and ‘K’ will remain the same for King.
Note: If there are multiple positions of pieces to trap the King by following all conditions, Then just print any of the Valid positions of the piece(s) of the used set to trap.
Examples:
Input: matrix[][] =
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * K * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *Output:
* * * * * * * *
* * * * * * * *
* * * * * Q * *
* * * K * * * *
* R * * * * * *
* * * * B * * *
* * * * * * * *
* * * * * * * *Explanation: King is present at (4, 4), Queen at (3, 6), Rook at (5, 2) and Bishop at (6, 5) .Trapping the king is not possible by Set 1 and 2, Therefore Set 3 is used.King is not under-attack by any of the pieces as well as not able to make any of its possible move.Hence, all the conditions are met and one of the possible position of pieces is present in output.
Input: matrix[][] =
* * * * * * * *
* K * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *Output:
* * * Q * * * *
* K * * * * * *
* * * R * * * *
* * B * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *Explanation: King is present at (2, 2), Queen at (1, 4), Rook at (3, 4) and Bishop at (4, 3) .It can be verified that trapping the king is not possible by Set 1 and 2, Therefore, Set 3 is used.King is not under-attack by any of the pieces as well as not able to make any of its possible move.Hence, all the conditions are met and one of the possible position of pieces is present in output.
Approach: The problem is purely based on observations. See the Below explanation of all observations deeply and where to use all three provided sets of pieces.
Observations:
These are all the possible cases for King’s position; Different colored squares required different sets and positions of pieces. So, the King can’t make any its possible move. Let’s see the set used by each colored square to trap the King:-
- For Dark Green colored squares:-
If the King is present at one of the Dark Green colored squares, It is always possible to trap it by using Set 1.
- For Orange colored squares:-
If the King is present at one of the Orange colored squares, It is not possible to trap King by Set 1, therefore Set 2 can be used.
- For Sky, Light Green and Brown colored squares:-
If the King is present one of the Sky Blue, Light Green or Brown colored squares, It is not possible to trap King by Set 1 or 2, So, Set 3 can be used.
Note: Instead having use of same set(Set 3 for Sky-Blue/Light Green/Brown); different colors are used because It would be difficult to find and apply the same valid position of pieces in all these colored squares.That’s why these different three colors are used, In which all three colored squares will use Set 3 to trap but different arrangement of position of pieces. So, that code can be implemented easily. Below one example of each color cell is provided.
One Example of each colored cell:
- For Dark Green Square:
- For Orange Square:
- For Sky Blue Square:
- For light Green Squares:
- For Brown Square:
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // User defined Function for // printing chessboard[][] void printer(vector<vector< char >> chessboard) { for ( int i = 1; i < chessboard.size(); i++) { for ( int j = 1; j < chessboard[0].size(); j++) { cout<<chessboard[i][j] << " " ; } cout<<endl; } } // Driver Function int main() { // Input Matrix vector<vector< char >> matrix = { { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , 'K' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' } }; // Variables to Store // position of King int a = 0, b = 0; // Outer loop for traversing // on input matrix[][] for ( int i = 0; i < matrix.size(); i++) { // Inner loop for traversing // on matrix[][] for ( int j = 0; j < matrix[0].size(); j++) { // If Position of // King found if (matrix[i][j] == 'K' ) { // Updating variables // if 'K'(position of // King) is found a = i + 1; b = j + 1; } } } // 2D chessboard array initialized vector<vector< char >> chessboard(9, vector< char >(9)); // Updating whole chessboard with '*' for ( int i = 0; i < 9; i++) for ( int j = 0; j < 9; j++) chessboard[i][j] = '*' ; // King's position stored as char 'K' in // chessboard[][] chessboard[a][b] = 'K' ; // for All Dark Green // colored Squares if (a == 1 && b == 1) { chessboard[2][3] = 'Q' ; } else if (a == 1 && b == 8) { chessboard[2][6] = 'Q' ; } else if (a == 8 && b == 1) { chessboard[7][3] = 'Q' ; } else if (a == 8 && b == 8) { chessboard[7][6] = 'Q' ; } // For all Orange colored Squares else if (b == 1) { chessboard[a - 1][3] = 'Q' ; chessboard[a + 1][3] = 'R' ; } else if (a == 1) { chessboard[3][b - 1] = 'Q' ; chessboard[3][b + 1] = 'R' ; } else if (b == 8) { chessboard[a - 1][6] = 'Q' ; chessboard[a + 1][6] = 'R' ; } else if (a == 8) { chessboard[6][b - 1] = 'Q' ; chessboard[6][b + 1] = 'R' ; } // For all Sky Blue colored Squares else if (a == 2 && b == 2) { chessboard[1][4] = 'Q' ; chessboard[3][4] = 'R' ; chessboard[4][3] = 'B' ; } else if (a == 2 && b == 7) { chessboard[1][5] = 'Q' ; chessboard[3][5] = 'R' ; chessboard[4][6] = 'B' ; } else if (a == 7 && b == 2) { chessboard[8][4] = 'Q' ; chessboard[6][4] = 'R' ; chessboard[5][3] = 'B' ; } else if (a == 7 && b == 7) { chessboard[8][5] = 'Q' ; chessboard[6][5] = 'R' ; chessboard[5][6] = 'B' ; } // For all light Green // colored Squares else if (a == 2 || a == 7 || b == 2 || b == 7) { if (a == 2) { chessboard[a - 1][b + 2] = 'Q' ; chessboard[a + 2][b - 1] = 'R' ; chessboard[a + 2][b] = 'B' ; } else if (a == 7) { chessboard[a + 1][b + 2] = 'Q' ; chessboard[a - 2][b - 1] = 'R' ; chessboard[a - 2][b] = 'B' ; } else if (b == 2) { chessboard[a - 1][b + 2] = 'Q' ; chessboard[a + 1][b + 2] = 'R' ; chessboard[a + 2][b + 1] = 'B' ; } else { chessboard[a - 1][b - 2] = 'Q' ; chessboard[a + 1][b - 2] = 'R' ; chessboard[a + 2][b - 1] = 'B' ; } } // For all Brown colored Squares else { chessboard[a - 1][b + 2] = 'Q' ; chessboard[a + 1][b - 2] = 'R' ; chessboard[a + 2][b + 1] = 'B' ; } // Function to print chessboard // along with position of Pieces printer(chessboard); } // This code is contributed by Potta Lokesh |
Java
// Java code to implement the approach import java.util.*; class GFG { // Driver Function public static void main(String[] args) { // Input Matrix char matrix[][] = { { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , 'K' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' } }; // Variables to Store // position of King int a = 0 , b = 0 ; // Outer loop for traversing // on input matrix[][] for ( int i = 0 ; i < matrix.length; i++) { // Inner loop for traversing // on matrix[][] for ( int j = 0 ; j < matrix[ 0 ].length; j++) { // If Position of // King found if (matrix[i][j] == 'K' ) { // Updating variables // if 'K'(position of // King) is found a = i + 1 ; b = j + 1 ; } } } // 2D chessboard array initialized char [][] chessboard = new char [ 9 ][ 9 ]; //Updating whole chessboard with '*' for ( int i = 0 ; i < 9 ; i++) for ( int j = 0 ; j < 9 ; j++) chessboard[i][j] = '*' ; // King's position stored as char 'K' in // chessboard[][] chessboard[a][b] = 'K' ; // for All Dark Green // colored Squares if (a == 1 && b == 1 ) { chessboard[ 2 ][ 3 ] = 'Q' ; } else if (a == 1 && b == 8 ) { chessboard[ 2 ][ 6 ] = 'Q' ; } else if (a == 8 && b == 1 ) { chessboard[ 7 ][ 3 ] = 'Q' ; } else if (a == 8 && b == 8 ) { chessboard[ 7 ][ 6 ] = 'Q' ; } // For all Orange colored Squares else if (b == 1 ) { chessboard[a - 1 ][ 3 ] = 'Q' ; chessboard[a + 1 ][ 3 ] = 'R' ; } else if (a == 1 ) { chessboard[ 3 ][b - 1 ] = 'Q' ; chessboard[ 3 ][b + 1 ] = 'R' ; } else if (b == 8 ) { chessboard[a - 1 ][ 6 ] = 'Q' ; chessboard[a + 1 ][ 6 ] = 'R' ; } else if (a == 8 ) { chessboard[ 6 ][b - 1 ] = 'Q' ; chessboard[ 6 ][b + 1 ] = 'R' ; } // For all Sky Blue colored Squares else if (a == 2 && b == 2 ) { chessboard[ 1 ][ 4 ] = 'Q' ; chessboard[ 3 ][ 4 ] = 'R' ; chessboard[ 4 ][ 3 ] = 'B' ; } else if (a == 2 && b == 7 ) { chessboard[ 1 ][ 5 ] = 'Q' ; chessboard[ 3 ][ 5 ] = 'R' ; chessboard[ 4 ][ 6 ] = 'B' ; } else if (a == 7 && b == 2 ) { chessboard[ 8 ][ 4 ] = 'Q' ; chessboard[ 6 ][ 4 ] = 'R' ; chessboard[ 5 ][ 3 ] = 'B' ; } else if (a == 7 && b == 7 ) { chessboard[ 8 ][ 5 ] = 'Q' ; chessboard[ 6 ][ 5 ] = 'R' ; chessboard[ 5 ][ 6 ] = 'B' ; } // For all light Green // colored Squares else if (a == 2 || a == 7 || b == 2 || b == 7 ) { if (a == 2 ) { chessboard[a - 1 ][b + 2 ] = 'Q' ; chessboard[a + 2 ][b - 1 ] = 'R' ; chessboard[a + 2 ][b] = 'B' ; } else if (a == 7 ) { chessboard[a + 1 ][b + 2 ] = 'Q' ; chessboard[a - 2 ][b - 1 ] = 'R' ; chessboard[a - 2 ][b] = 'B' ; } else if (b == 2 ) { chessboard[a - 1 ][b + 2 ] = 'Q' ; chessboard[a + 1 ][b + 2 ] = 'R' ; chessboard[a + 2 ][b + 1 ] = 'B' ; } else { chessboard[a - 1 ][b - 2 ] = 'Q' ; chessboard[a + 1 ][b - 2 ] = 'R' ; chessboard[a + 2 ][b - 1 ] = 'B' ; } } // For all Brown colored Squares else { chessboard[a - 1 ][b + 2 ] = 'Q' ; chessboard[a + 1 ][b - 2 ] = 'R' ; chessboard[a + 2 ][b + 1 ] = 'B' ; } // Function to print chessboard // along with position of Pieces printer(chessboard); } // User defined Function for // printing chessboard[][] static void printer( char [][] chessboard) { for ( int i = 1 ; i < chessboard.length; i++) { for ( int j = 1 ; j < chessboard[ 0 ].length; j++) { System.out.print(chessboard[i][j] + " " ); } System.out.println(); } } } |
Python3
class GFG : # Driver Function @staticmethod def main( args) : # Input Matrix matrix = [[ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , 'K' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ]] # Variables to Store # position of King a = 0 b = 0 # Outer loop for traversing # on input matrix[][] i = 0 while (i < len (matrix)) : # Inner loop for traversing # on matrix[][] j = 0 while (j < len (matrix[ 0 ])) : # If Position of # King found if (matrix[i][j] = = 'K' ) : # Updating variables # if 'K'(position of # King) is found a = i + 1 b = j + 1 j + = 1 i + = 1 # 2D chessboard array initialized chessboard = [[ ' ' ] * ( 9 ) for _ in range ( 9 )] # Updating whole chessboard with '*' i = 0 while (i < 9 ) : j = 0 while (j < 9 ) : chessboard[i][j] = '*' j + = 1 i + = 1 # King's position stored as char 'K' in # chessboard[][] chessboard[a][b] = 'K' # for All Dark Green # colored Squares if (a = = 1 and b = = 1 ) : chessboard[ 2 ][ 3 ] = 'Q' elif (a = = 1 and b = = 8 ) : chessboard[ 2 ][ 6 ] = 'Q' elif (a = = 8 and b = = 1 ) : chessboard[ 7 ][ 3 ] = 'Q' elif (a = = 8 and b = = 8 ) : chessboard[ 7 ][ 6 ] = 'Q' elif (b = = 1 ) : chessboard[a - 1 ][ 3 ] = 'Q' chessboard[a + 1 ][ 3 ] = 'R' elif (a = = 1 ) : chessboard[ 3 ][b - 1 ] = 'Q' chessboard[ 3 ][b + 1 ] = 'R' elif (b = = 8 ) : chessboard[a - 1 ][ 6 ] = 'Q' chessboard[a + 1 ][ 6 ] = 'R' elif (a = = 8 ) : chessboard[ 6 ][b - 1 ] = 'Q' chessboard[ 6 ][b + 1 ] = 'R' elif (a = = 2 and b = = 2 ) : chessboard[ 1 ][ 4 ] = 'Q' chessboard[ 3 ][ 4 ] = 'R' chessboard[ 4 ][ 3 ] = 'B' elif (a = = 2 and b = = 7 ) : chessboard[ 1 ][ 5 ] = 'Q' chessboard[ 3 ][ 5 ] = 'R' chessboard[ 4 ][ 6 ] = 'B' elif (a = = 7 and b = = 2 ) : chessboard[ 8 ][ 4 ] = 'Q' chessboard[ 6 ][ 4 ] = 'R' chessboard[ 5 ][ 3 ] = 'B' elif (a = = 7 and b = = 7 ) : chessboard[ 8 ][ 5 ] = 'Q' chessboard[ 6 ][ 5 ] = 'R' chessboard[ 5 ][ 6 ] = 'B' elif (a = = 2 or a = = 7 or b = = 2 or b = = 7 ) : if (a = = 2 ) : chessboard[a - 1 ][b + 2 ] = 'Q' chessboard[a + 2 ][b - 1 ] = 'R' chessboard[a + 2 ][b] = 'B' elif (a = = 7 ) : chessboard[a + 1 ][b + 2 ] = 'Q' chessboard[a - 2 ][b - 1 ] = 'R' chessboard[a - 2 ][b] = 'B' elif (b = = 2 ) : chessboard[a - 1 ][b + 2 ] = 'Q' chessboard[a + 1 ][b + 2 ] = 'R' chessboard[a + 2 ][b + 1 ] = 'B' else : chessboard[a - 1 ][b - 2 ] = 'Q' chessboard[a + 1 ][b - 2 ] = 'R' chessboard[a + 2 ][b - 1 ] = 'B' else : chessboard[a - 1 ][b + 2 ] = 'Q' chessboard[a + 1 ][b - 2 ] = 'R' chessboard[a + 2 ][b + 1 ] = 'B' # Function to print chessboard # along with position of Pieces GFG.printer(chessboard) # User defined Function for # printing chessboard[][] @staticmethod def printer( chessboard) : i = 1 while (i < len (chessboard)) : j = 1 while (j < len (chessboard[ 0 ])) : print ( str (chessboard[i][j]) + " " , end = "") j + = 1 print () i + = 1 if __name__ = = "__main__" : GFG.main([]) # This code is contributed by aadityaburujwale. |
C#
// Include namespace system using System; public class GFG { // Driver Function public static void Main(String[] args) { // Input Matrix char [, ] matrix = { { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , 'K' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' }, { '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' } }; // Variables to Store // position of King var a = 0; var b = 0; // Outer loop for traversing // on input matrix[][] for ( int i = 0; i < matrix.GetLength(0); i++) { // Inner loop for traversing // on matrix[][] for ( int j = 0; j < matrix.GetLength(1); j++) { // If Position of // King found if (matrix[i, j] == 'K' ) { // Updating variables // if 'K'(position of // King) is found a = i + 1; b = j + 1; } } } // 2D chessboard array initialized char [, ] chessboard = new char [9, 9]; // Updating whole chessboard with '*' for ( int i = 0; i < 9; i++) { for ( int j = 0; j < 9; j++) { chessboard[i, j] = '*' ; } } // King's position stored as char 'K' in // chessboard[][] chessboard[a, b] = 'K' ; // for All Dark Green // colored Squares if (a == 1 && b == 1) { chessboard[2, 3] = 'Q' ; } else if (a == 1 && b == 8) { chessboard[2, 6] = 'Q' ; } else if (a == 8 && b == 1) { chessboard[7, 3] = 'Q' ; } else if (a == 8 && b == 8) { chessboard[7, 6] = 'Q' ; } else if (b == 1) { chessboard[a - 1, 3] = 'Q' ; chessboard[a + 1, 3] = 'R' ; } else if (a == 1) { chessboard[3, b - 1] = 'Q' ; chessboard[3, b + 1] = 'R' ; } else if (b == 8) { chessboard[a - 1, 6] = 'Q' ; chessboard[a + 1, 6] = 'R' ; } else if (a == 8) { chessboard[6, b - 1] = 'Q' ; chessboard[6, b + 1] = 'R' ; } else if (a == 2 && b == 2) { chessboard[1, 4] = 'Q' ; chessboard[3, 4] = 'R' ; chessboard[4, 3] = 'B' ; } else if (a == 2 && b == 7) { chessboard[1, 5] = 'Q' ; chessboard[3, 5] = 'R' ; chessboard[4, 6] = 'B' ; } else if (a == 7 && b == 2) { chessboard[8, 4] = 'Q' ; chessboard[6, 4] = 'R' ; chessboard[5, 3] = 'B' ; } else if (a == 7 && b == 7) { chessboard[8, 5] = 'Q' ; chessboard[6, 5] = 'R' ; chessboard[5, 6] = 'B' ; } else if (a == 2 || a == 7 || b == 2 || b == 7) { if (a == 2) { chessboard[a - 1, b + 2] = 'Q' ; chessboard[a + 2, b - 1] = 'R' ; chessboard[a + 2, b] = 'B' ; } else if (a == 7) { chessboard[a + 1, b + 2] = 'Q' ; chessboard[a - 2, b - 1] = 'R' ; chessboard[a - 2, b] = 'B' ; } else if (b == 2) { chessboard[a - 1, b + 2] = 'Q' ; chessboard[a + 1, b + 2] = 'R' ; chessboard[a + 2, b + 1] = 'B' ; } else { chessboard[a - 1, b - 2] = 'Q' ; chessboard[a + 1, b - 2] = 'R' ; chessboard[a + 2, b - 1] = 'B' ; } } else { chessboard[a - 1, b + 2] = 'Q' ; chessboard[a + 1, b - 2] = 'R' ; chessboard[a + 2, b + 1] = 'B' ; } // Function to print chessboard // along with position of Pieces GFG.printer(chessboard); } // User defined Function for // printing chessboard[][] public static void printer( char [,] chessboard) { for ( int i = 1; i < 9; i++) { for ( int j = 1; j < 9; j++) { Console.Write(chessboard[i, j].ToString() + " " ); } Console.WriteLine(); } } } // This code is contributed by aadityaburujwale. |
Javascript
// JavaScript code to implement the approach function printer(chessboard){ for (let i = 1; chessboard.length; i++){ for (let j = 1; j < chessboard[0].length; j++){ console.log(chessboard[i][j] + " " ); } console.log( "<br>" ); } } // Input Matrix let matrix = [[ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , 'K' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ], [ '*' , '*' , '*' , '*' , '*' , '*' , '*' , '*' ]]; // Variables to Store // position of King let a = 0; let b = 0; // Outer loop for traversing // on input matrix[][] for (let i=0;i<matrix.length;i++){ // Inner loop for traversing // on matrix[][] for (let j=0;j<matrix[0].length;j++){ // If Position of // King found if (matrix[i][j]== 'K' ){ // Updating variables // if 'K'(position of // King) is found a = i + 1; b = j + 1; } } } // 2D chessboard array initialized var chessboard = []; // Updating whole chessboard with '*' for (let i = 0; i < 9; i++) { chessboard[i] = []; for (let j = 0; j < 9; j++) { chessboard[i][j] = '*' ; } } // King's position stored as char 'K' in // chessboard[][] chessboard[a][b] = 'K '; // for All Dark Green // colored Squares if (a == 1 && b == 1) { chessboard[2][3] = ' Q '; } else if (a == 1 && b == 8) { chessboard[2][6] = ' Q '; } else if (a == 8 && b == 1) { chessboard[7][3] = ' Q '; } else if (a == 8 && b == 8) { chessboard[7][6] = ' Q '; } // For all Orange colored Squares else if (b == 1) { chessboard[a - 1][3] = ' Q '; chessboard[a + 1][3] = ' R '; } else if (a == 1) { chessboard[3][b - 1] = ' Q '; chessboard[3][b + 1] = ' R '; } else if (b == 8) { chessboard[a - 1][6] = ' Q '; chessboard[a + 1][6] = ' R '; } else if (a == 8) { chessboard[6][b - 1] = ' Q '; chessboard[6][b + 1] = ' R '; } // For all Sky Blue colored Squares else if (a == 2 && b == 2) { chessboard[1][4] = ' Q '; chessboard[3][4] = ' R '; chessboard[4][3] = ' B '; } else if (a == 2 && b == 7) { chessboard[1][5] = ' Q '; chessboard[3][5] = ' R '; chessboard[4][6] = ' B '; } else if (a == 7 && b == 2) { chessboard[8][4] = ' Q '; chessboard[6][4] = ' R '; chessboard[5][3] = ' B '; } else if (a == 7 && b == 7) { chessboard[8][5] = ' Q '; chessboard[6][5] = ' R '; chessboard[5][6] = ' B '; } // For all light Green // colored Squares else if (a == 2 || a == 7 || b == 2 || b == 7) { if (a == 2) { chessboard[a - 1][b + 2] = ' Q '; chessboard[a + 2][b - 1] = ' R '; chessboard[a + 2][b] = ' B '; } else if (a == 7) { chessboard[a + 1][b + 2] = ' Q '; chessboard[a - 2][b - 1] = ' R '; chessboard[a - 2][b] = ' B '; } else if (b == 2) { chessboard[a - 1][b + 2] = ' Q '; chessboard[a + 1][b + 2] = ' R '; chessboard[a + 2][b + 1] = ' B '; } else { chessboard[a - 1][b - 2] = ' Q '; chessboard[a + 1][b - 2] = ' R '; chessboard[a + 2][b - 1] = ' B '; } } // For all Brown colored Squares else { chessboard[a - 1][b + 2] = ' Q '; chessboard[a + 1][b - 2] = ' R '; chessboard[a + 2][b + 1] = ' B'; } // Function to print chessboard // along with position of Pieces printer(chessboard); // This code is contributed by lokeshmvs21. |
* * * * * * * * * * * * * * * * * * * * * Q * * * * * K * * * * * R * * * * * * * * * * B * * * * * * * * * * * * * * * * * * *
Time Complexity: O(1), As only constant operations are made.
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!