Queen Blocks Codechef Solution

Hello coders, today we are going to solve the Queen Blocks Codechef Solution whose Problem Code is QUEENBL.

As you already know that this site does not contain only the Codefchef solutions here, you can also find the solution for other programming problems. I.e. LeetcodeC programsC++ Programs SolutionsPython ProgramsWeb Technology, Data StructuresRDBMS Programs, and Java Programs Solutions.

Queen Blocks Codechef Solution
Queen Blocks Codechef Solution

Queen Blocks Codechef Solution

Problem

You are given an 8×8 chessboard. Your enemy king is at point (X, Y) (1 ≤ X, Y ≤ 8).

You need to place some number of queen pieces on the chessboard such that the following conditions are satisfied:

  • The king should not be under attack by any of the queens that you have placed.
  • The king should not be able to make any move.

Note that a king can move one unit in any of the 8 direction while a queen can attack horizontally, vertically as well as diagonally.

Find the minimum number of queens required to satisfy the given conditions. Print the chessboard where 0 denotes empty cells, 1 denotes the king, and 2 denotes the queen(s) placed by you in some position(s).

In case of multiple answers, print any.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • The only line of each test case contains two space-separated integers A and B — the coordinates of the king.

Output Format

For each test case, output a 8×8 grid consisting of King, Queen(s) and blank cells.

Print the chessboard in 8 lines where each line has 8 space-separated integers where 0 denotes empty cells, 1 denotes the king, and 2 denotes the queen(s) placed by you in some position(s).

Constraints

1 ≤ T ≤ 64
1 ≤ A, B ≤ 8

Sample 1

Input

1
2
1

Output

0 0 2 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

LinkedIn Skill Assessment Answers

Coursera Quiz Answers

Explanation:

Test case 11: We can place 22 queens at (1, 3)(1,3) and (3, 3)(3,3) respectively. Thus, the king at (2, 1)(2,1) has no safe squares to go to and is not under attack.

Queen Blocks Codechef Solution


Queen Blocks Codechef Solution in CPP

#include<bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
int main()
{
    int x,y,i,j,k,t,n;
    cin >> t ;
    while(t--){
        cin >> x >> y ;
        int chess[9][9] = {} ;
        chess[x][y] = 1 ;
        if((x==1||x==8)&&(y==1||y==8)){
            x++; y+=2;
            if(x>8)
                x -=2;
            if(y>8)
                y -=4;
            chess[x][y] = 2;
        }
        else if(x==1 || y==1){
            if(x==1){
                chess[3][y+1] =2;
                chess[3][y-1] =2;
            }
            else {
                chess[x-1][3] = 2;
                chess[x+1][3] = 2;
            }
        }
        else if(x==8||y==8){
            if(x==8){
                chess[6][y+1] = 2;
                chess[6][y-1] = 2;
            }
            else {
                chess[x+1][6] = 2;
                chess[x-1][6] = 2;
            }
        }
        else {
            if((x+2<=8)&&(y+3<=8)){
                chess[x+2][y-1] = 2;
                chess[x-1][y+3] = 2;
            }
            else if(x+2<=8){
                chess[x+2][y+1] = 2;
                chess[x-1][y-3] = 2;
            }
            else if(y+3<=8){
                chess[x-2][y-1] = 2;
                chess[x+1][y+3] = 2;
            }
            else {
                chess[x-2][y+1] = 2;
                chess[x+1][y-3] = 2;
            }
        }
        /// output ...
        for(i=1;i<9;i++){
            for(j=1;j<9;j++)
                cout << chess[i][j] << " " ;
            cout << endl ;
        }
    }
    return 0;
}
       

Queen Blocks Codechef Solution in JAVA

/* package codechef; // don't place package name! */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class Prob2 {
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        int T=Integer.parseInt(br.readLine());
        String s[];
        int x,y;
        for(int i=0;i<T;i++)
        {
            s=br.readLine().split(" ");
            x=Integer.parseInt(s[0]);
            y=Integer.parseInt(s[1]);
            int Matrix[][]={{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};
            Matrix[x-1][y-1]=1;
            //Corners
            if(x==1 && y==1)
            {
                Matrix[x][y+1]=2;
            }
            else if(x==1 && y==8)
            {
                Matrix[x+1][y-2]=2;
            }
            else if(x==8 && y==1)
            {
                Matrix[x-2][y+1]=2;
            }
            else if(x==8 && y==8)
            {
                Matrix[x-2][y-3]=2;
            }
            //Last row or column
            else if(x==1)
            {
                Matrix[x+1][y]=2;
                Matrix[x+1][y-2]=2;
            }
            else if(x==8)
            {
                Matrix[x-3][y-2]=2;
                Matrix[x-3][y]=2;
            }
            else if(y==1)
            {
                Matrix[x][y+1]=2;
                Matrix[x-2][y+1]=2;
            }
            else if(y==8)
            {
                Matrix[x][y-3]=2;
                Matrix[x-2][y-3]=2;
            }
            //One Square diagonally above corner
            else if(x-1==1 && y-1==1)
            {
                Matrix[3][0]=2;
                Matrix[0][4]=2;
            }
            else if(x-1==1 && y+1==8)
            {
                Matrix[3][7]=2;
                Matrix[0][3]=2;
            }
            else if(x+1==8 && y-1==1)
            {
                Matrix[7][3]=2;
                Matrix[3][0]=2;
            }
            else if(x+1==8 && y+1==8)
            {
                Matrix[7][3]=2;
                Matrix[4][7]=2;
            }
            else
            {
                if((x<8 && y+1<8) && ((x-2>=0 && y-3>=0)))
                {
                    Matrix[x][y+1]=2;
                    Matrix[x-2][y-3]=2;
                }
                else
                {
                    Matrix[x+1][y]=2;
                    Matrix[x-3][y-2]=2;
                }
            }
            for(int j=0;j<8;j++)
            {
                for(int k=0;k<8;k++)
                {
                    pw.print(Matrix[j][k]+" ");
                }
                pw.println();
            }
        }
        pw.flush();
    }
}

Queen Blocks Codechef Solution in Python

# Chase2learn
# cook your dish here
for _ in range(int(input())):
    x,y=map(int,input().split())
    board=[]
    for i in range(8):
        temp=[]
        for j in range(8):
            temp.append(0)
        board.append(temp)
    a=[]
    if (x==1 or x==8) and (y==1 or y==8):
        if x==1:
            if y==1:
                a.append([3,2])
            else:
                a.append([3,7])
        else:
            if y==1:
                a.append([6,2])
            else:
                a.append([6,7])
    elif x==1:
        a.append([3,y-1])
        a.append([3,y+1])
    elif x==8:
        a.append([6,y-1])
        a.append([6,y+1])
    elif y==1:
        a.append([x-1,3])
        a.append([x+1,3])
    elif y==8:
        a.append([x-1,6])
        a.append([x+1,6])
    else:
        if x==2:
            if y==2:
                a.append([x+3,y-1])
                a.append([x-1,y+2])
            elif y==7:
                a.append([x+3,y+1])
                a.append([x-1,y-2])
            else:
                a.append([x+1,y-2])
                a.append([x-1,y+2])
        elif x==7:
            if y==2:
                a.append([x-3,y-1])
                a.append([x+1,y+2])
            elif y==7:
                a.append([x-3,y+1])
                a.append([x+1,y-2])
            else:
                a.append([x+1,y-2])
                a.append([x-1,y+2])
        else:
            a.append([x+2,y-1])
            a.append([x-2,y+1])
    board[x-1][y-1]=1
    # print(a)
    for i in a:
        board[i[0]-1][i[1]-1]=2
    for i in range(8):
        for j in range(8):
            print(board[i][j],end=" ")
        print("")
    

Disclaimer: The above Problem (Queen Blocks) is generated by CodeChef but the solution is provided by  Chase2learn.This tutorial is only for Educational and Learning purpose.

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Leave a Comment