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 an8 X 8 matrix as output, in which 'Q' will representQueen, 'R' will represent Rook, 'B' will represent 'Bishop', according to the set usedand '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.
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.
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:
All possible positions pf King
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:
An Example of Dark Green Square
For Orange Square:
An example of Orange colored Square
For Sky Blue Square:
An Example of Sky Blue Colored Square
For light Green Squares:
An Example of Light Green Square
For Brown Square:
An Example of Brown colored square
Below is the implementation of the above approach:
C++
// C++ code to implement the approach#include<bits/stdc++.h>usingnamespacestd;// User defined Function for// printing chessboard[][]voidprinter(vector<vector<char>>chessboard){for(inti=1;i<chessboard.size();i++){for(intj=1;j<chessboard[0].size();j++){cout<<chessboard[i][j]<<" ";}cout<<endl;}}// Driver Functionintmain(){// Input Matrixvector<vector<char>>matrix={{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','K','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'}};// Variables to Store// position of Kinginta=0,b=0;// Outer loop for traversing// on input matrix[][]for(inti=0;i<matrix.size();i++){// Inner loop for traversing// on matrix[][]for(intj=0;j<matrix[0].size();j++){// If Position of// King foundif(matrix[i][j]=='K'){// Updating variables// if 'K'(position of// King) is founda=i+1;b=j+1;}}}// 2D chessboard array initializedvector<vector<char>>chessboard(9,vector<char>(9));// Updating whole chessboard with '*'for(inti=0;i<9;i++)for(intj=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 Squaresif(a==1&&b==1){chessboard[2][3]='Q';}elseif(a==1&&b==8){chessboard[2][6]='Q';}elseif(a==8&&b==1){chessboard[7][3]='Q';}elseif(a==8&&b==8){chessboard[7][6]='Q';}// For all Orange colored Squareselseif(b==1){chessboard[a-1][3]='Q';chessboard[a+1][3]='R';}elseif(a==1){chessboard[3][b-1]='Q';chessboard[3][b+1]='R';}elseif(b==8){chessboard[a-1][6]='Q';chessboard[a+1][6]='R';}elseif(a==8){chessboard[6][b-1]='Q';chessboard[6][b+1]='R';}// For all Sky Blue colored Squareselseif(a==2&&b==2){chessboard[1][4]='Q';chessboard[3][4]='R';chessboard[4][3]='B';}elseif(a==2&&b==7){chessboard[1][5]='Q';chessboard[3][5]='R';chessboard[4][6]='B';}elseif(a==7&&b==2){chessboard[8][4]='Q';chessboard[6][4]='R';chessboard[5][3]='B';}elseif(a==7&&b==7){chessboard[8][5]='Q';chessboard[6][5]='R';chessboard[5][6]='B';}// For all light Green// colored Squareselseif(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';}elseif(a==7){chessboard[a+1][b+2]='Q';chessboard[a-2][b-1]='R';chessboard[a-2][b]='B';}elseif(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 Squareselse{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 Piecesprinter(chessboard);}// This code is contributed by Potta Lokesh
Java
// Java code to implement the approachimportjava.util.*;classGFG{// Driver Functionpublicstaticvoidmain(String[]args){// Input Matrixcharmatrix[][]={{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','K','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'}};// Variables to Store// position of Kinginta=0,b=0;// Outer loop for traversing// on input matrix[][]for(inti=0;i<matrix.length;i++){// Inner loop for traversing// on matrix[][]for(intj=0;j<matrix[0].length;j++){// If Position of// King foundif(matrix[i][j]=='K'){// Updating variables// if 'K'(position of// King) is founda=i+1;b=j+1;}}}// 2D chessboard array initializedchar[][]chessboard=newchar[9][9];//Updating whole chessboard with '*'for(inti=0;i<9;i++)for(intj=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 Squaresif(a==1&&b==1){chessboard[2][3]='Q';}elseif(a==1&&b==8){chessboard[2][6]='Q';}elseif(a==8&&b==1){chessboard[7][3]='Q';}elseif(a==8&&b==8){chessboard[7][6]='Q';}// For all Orange colored Squareselseif(b==1){chessboard[a-1][3]='Q';chessboard[a+1][3]='R';}elseif(a==1){chessboard[3][b-1]='Q';chessboard[3][b+1]='R';}elseif(b==8){chessboard[a-1][6]='Q';chessboard[a+1][6]='R';}elseif(a==8){chessboard[6][b-1]='Q';chessboard[6][b+1]='R';}// For all Sky Blue colored Squareselseif(a==2&&b==2){chessboard[1][4]='Q';chessboard[3][4]='R';chessboard[4][3]='B';}elseif(a==2&&b==7){chessboard[1][5]='Q';chessboard[3][5]='R';chessboard[4][6]='B';}elseif(a==7&&b==2){chessboard[8][4]='Q';chessboard[6][4]='R';chessboard[5][3]='B';}elseif(a==7&&b==7){chessboard[8][5]='Q';chessboard[6][5]='R';chessboard[5][6]='B';}// For all light Green// colored Squareselseif(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';}elseif(a==7){chessboard[a+1][b+2]='Q';chessboard[a-2][b-1]='R';chessboard[a-2][b]='B';}elseif(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 Squareselse{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 Piecesprinter(chessboard);}// User defined Function for// printing chessboard[][]staticvoidprinter(char[][]chessboard){for(inti=1;i<chessboard.length;i++){for(intj=1;j<chessboard[0].length;j++){System.out.print(chessboard[i][j]+" ");}System.out.println();}}}
Python3
classGFG:# Driver Function@staticmethoddefmain(args):# Input Matrixmatrix=[['*','*','*','*','*','*','*','*'],['*','*','*','*','*','*','*','*'],['*','*','*','*','*','*','*','*'],['*','*','*','K','*','*','*','*'],['*','*','*','*','*','*','*','*'],['*','*','*','*','*','*','*','*'],['*','*','*','*','*','*','*','*'],['*','*','*','*','*','*','*','*']]# Variables to Store# position of Kinga=0b=0# Outer loop for traversing# on input matrix[][]i=0while(i<len(matrix)):# Inner loop for traversing# on matrix[][]j=0while(j<len(matrix[0])):# If Position of# King foundif(matrix[i][j]=='K'):# Updating variables# if 'K'(position of# King) is founda=i+1b=j+1j+=1i+=1# 2D chessboard array initializedchessboard=[[' ']*(9)for_inrange(9)]# Updating whole chessboard with '*'i=0while(i<9):j=0while(j<9):chessboard[i][j]='*'j+=1i+=1# King's position stored as char 'K' in# chessboard[][]chessboard[a][b]='K'# for All Dark Green# colored Squaresif(a==1andb==1):chessboard[2][3]='Q'elif(a==1andb==8):chessboard[2][6]='Q'elif(a==8andb==1):chessboard[7][3]='Q'elif(a==8andb==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==2andb==2):chessboard[1][4]='Q'chessboard[3][4]='R'chessboard[4][3]='B'elif(a==2andb==7):chessboard[1][5]='Q'chessboard[3][5]='R'chessboard[4][6]='B'elif(a==7andb==2):chessboard[8][4]='Q'chessboard[6][4]='R'chessboard[5][3]='B'elif(a==7andb==7):chessboard[8][5]='Q'chessboard[6][5]='R'chessboard[5][6]='B'elif(a==2ora==7orb==2orb==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 PiecesGFG.printer(chessboard)# User defined Function for# printing chessboard[][]@staticmethoddefprinter(chessboard):i=1while(i<len(chessboard)):j=1while(j<len(chessboard[0])):print(str(chessboard[i][j])+" ",end="")j+=1print()i+=1if__name__=="__main__":GFG.main([])# This code is contributed by aadityaburujwale.
C#
// Include namespace systemusingSystem;publicclassGFG{// Driver FunctionpublicstaticvoidMain(String[]args){// Input Matrixchar[,]matrix={{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','K','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'}};// Variables to Store// position of Kingvara=0;varb=0;// Outer loop for traversing// on input matrix[][]for(inti=0;i<matrix.GetLength(0);i++){// Inner loop for traversing// on matrix[][]for(intj=0;j<matrix.GetLength(1);j++){// If Position of// King foundif(matrix[i,j]=='K'){// Updating variables// if 'K'(position of// King) is founda=i+1;b=j+1;}}}// 2D chessboard array initializedchar[,]chessboard=newchar[9,9];// Updating whole chessboard with '*'for(inti=0;i<9;i++){for(intj=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 Squaresif(a==1&&b==1){chessboard[2,3]='Q';}elseif(a==1&&b==8){chessboard[2,6]='Q';}elseif(a==8&&b==1){chessboard[7,3]='Q';}elseif(a==8&&b==8){chessboard[7,6]='Q';}elseif(b==1){chessboard[a-1,3]='Q';chessboard[a+1,3]='R';}elseif(a==1){chessboard[3,b-1]='Q';chessboard[3,b+1]='R';}elseif(b==8){chessboard[a-1,6]='Q';chessboard[a+1,6]='R';}elseif(a==8){chessboard[6,b-1]='Q';chessboard[6,b+1]='R';}elseif(a==2&&b==2){chessboard[1,4]='Q';chessboard[3,4]='R';chessboard[4,3]='B';}elseif(a==2&&b==7){chessboard[1,5]='Q';chessboard[3,5]='R';chessboard[4,6]='B';}elseif(a==7&&b==2){chessboard[8,4]='Q';chessboard[6,4]='R';chessboard[5,3]='B';}elseif(a==7&&b==7){chessboard[8,5]='Q';chessboard[6,5]='R';chessboard[5,6]='B';}elseif(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';}elseif(a==7){chessboard[a+1,b+2]='Q';chessboard[a-2,b-1]='R';chessboard[a-2,b]='B';}elseif(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 PiecesGFG.printer(chessboard);}// User defined Function for// printing chessboard[][]publicstaticvoidprinter(char[,]chessboard){for(inti=1;i<9;i++){for(intj=1;j<9;j++){Console.Write(chessboard[i,j].ToString()+" ");}Console.WriteLine();}}}// This code is contributed by aadityaburujwale.
JavaScript
// JavaScript code to implement the approachfunctionprinter(chessboard){for(leti=1;chessboard.length;i++){for(letj=1;j<chessboard[0].length;j++){console.log(chessboard[i][j]+" ");}console.log("<br>");}}// Input Matrixletmatrix=[['*','*','*','*','*','*','*','*'],['*','*','*','*','*','*','*','*'],['*','*','*','*','*','*','*','*'],['*','*','*','K','*','*','*','*'],['*','*','*','*','*','*','*','*'],['*','*','*','*','*','*','*','*'],['*','*','*','*','*','*','*','*'],['*','*','*','*','*','*','*','*']];// Variables to Store// position of Kingleta=0;letb=0;// Outer loop for traversing// on input matrix[][]for(leti=0;i<matrix.length;i++){// Inner loop for traversing// on matrix[][]for(letj=0;j<matrix[0].length;j++){// If Position of// King foundif(matrix[i][j]=='K'){// Updating variables// if 'K'(position of// King) is founda=i+1;b=j+1;}}}// 2D chessboard array initializedvarchessboard=[];// Updating whole chessboard with '*'for(leti=0;i<9;i++){chessboard[i]=[];for(letj=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 Squaresif(a==1&&b==1){chessboard[2][3]='Q';}elseif(a==1&&b==8){chessboard[2][6]='Q';}elseif(a==8&&b==1){chessboard[7][3]='Q';}elseif(a==8&&b==8){chessboard[7][6]='Q';}// For all Orange colored Squareselseif(b==1){chessboard[a-1][3]='Q';chessboard[a+1][3]='R';}elseif(a==1){chessboard[3][b-1]='Q';chessboard[3][b+1]='R';}elseif(b==8){chessboard[a-1][6]='Q';chessboard[a+1][6]='R';}elseif(a==8){chessboard[6][b-1]='Q';chessboard[6][b+1]='R';}// For all Sky Blue colored Squareselseif(a==2&&b==2){chessboard[1][4]='Q';chessboard[3][4]='R';chessboard[4][3]='B';}elseif(a==2&&b==7){chessboard[1][5]='Q';chessboard[3][5]='R';chessboard[4][6]='B';}elseif(a==7&&b==2){chessboard[8][4]='Q';chessboard[6][4]='R';chessboard[5][3]='B';}elseif(a==7&&b==7){chessboard[8][5]='Q';chessboard[6][5]='R';chessboard[5][6]='B';}// For all light Green// colored Squareselseif(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';}elseif(a==7){chessboard[a+1][b+2]='Q';chessboard[a-2][b-1]='R';chessboard[a-2][b]='B';}elseif(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 Squareselse{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 Piecesprinter(chessboard);// This code is contributed by lokeshmvs21.