You are on page 1of 5

EXPERIMENT 3.

Name: Aman Kumar UID: 19BCS1498


Class: 19CSE-IS-4A DATE:27/04/2022
Subject Code: 22E-CSP-356

AIM:
(i) Write a program to implement N-queen problem
Problem link: https://www.hackerearth.com/practice/basic-programming/recursion/recursion-
and-backtracking/practice-problems/algorithm/n-queensrecursion-tutorial/
(ii) Write a program to implement Recursive digit sum problem.
Problem link: https://www.hackerrank.com/challenges/recursive-digit-
sum/problem?h_l=interview&isFullScreen=true&playlist_slugs%5B%5D%5B%5D=intervie
w-preparation-kit&playlist_slugs%5B%5D%5B%5D=recursion-backtracking
OVERVIEW OF THE PRACTICAL:
(i) Given a chess board having N×N cells, you need to place N queens on the board in such a way that no
queen attacks any other queen.

Input:
The only line of input consists of a single integer denoting N.

Output:
If it is possible to place all the N queens in such a way that no queen attacks another queen, then print N lines
having N integers. The integer in ith line and jth column will denote the cell (i,j) of the board and should be 1 if a
queen is placed at (i,j) otherwise 0. If there are more than way of placing queens print any of them. If it is not
possible to place all N queens in the desired way, then print "Not possible" (without quotes).

(ii) Function Description

Complete the function superDigit in the editor below. It must return the calculated super digit as an integer.

superDigit has the following parameter(s):

• string n: a string representation of an integer

• int k: the times to concatenate n to make p

Returns

• int: the super digit of n repeated k times


TASK TO BE DONE (LOGIC USED IN THE PROGRAM):
(i) Here in this solution i am using backtracking approach. In this problem we have to place a queens if possible
in each row such that it is at safe position. safe position here means: 1. there is no other queen in that same row
2. there is no other queen in that same coloumn. 3. there is no other queen present diagnoaly corresponding to
the current position.

for checking these three things i have made a funciton issafe(); and for solving the main problem
recursively(using backtracking) i have made a solve function.

(ii) We define super-digit function as following recursive function:

Super-digit(n) = n, n<10

= super-digit(sum-of-digit(n)), otherwise

Where sum-of-digit(m)=m, m=0

= (m mod 10) + sum-of-digit(m/10)

Note that since initially n can be very large, use a string representation to represent it for the first time.

MAIN CODE/FUNCTION OF PROGRAM:


(i) import java.util.Scanner;
public class NQueensRecursion {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[11][11];
int pos = 0;
if (n == 2 || n == 3) {
System.out.println("Not possible");
}
boolean b = nqueen(arr, n, pos);
}
public static boolean nqueen(int[][] arr, int n,int i) {
if (n==i){
//Successfully place the queens in n row position
//Print the rows
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (arr[j][k]==0){
System.out.print("0 ");
}
else{
System.out.print("1 ");
}
}
System.out.print("\n");
}
return true;
}
//Recursive Case
//Try to place the Queen in the Front row only rest will be handeled by the recursive
leap of faith
for (int j = 0; j < n; j++) {
//check if i,j position is safe to place the Queen or not
if (isSafe(arr,i,j,n)) {
arr[i][j] = 1;
boolean nextQueenRakhPaRaheHai = nqueen(arr,n, i + 1);
if (nextQueenRakhPaRaheHai) {
return true;
}
arr[i][j] = 0;
}
}

//You have tried for all position in the current row but couldn't place a queen
return false;

public static boolean isSafe(int[][] board, int i, int j, int n){


for(int row=0;row<n; row++){
if (board[row][j]==1){
return false;
}
}
int x = i;
int y = j;
while (x>=0 &&y>=0){
if (board[x][y]==1){
return false;
}
x--;
y--;
}
x = i;
y = j;
while (x>=0 && y<n){
if (board[x][y]==1){
return false;
}
x--;
y++;
}
return true;
}
}
(ii) class Result {

/*
* Complete the 'superDigit' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. STRING n
* 2. INTEGER k
*/

public static int superDigit(String number, int k) {


// Write your code here
if (number.length() > 1)
{
long sum = 0;
for (int i = 0; i < number.length(); i++)
{
sum += Character.getNumericValue(number.charAt(i));
}
return superDigit(Long.toString(sum * k), 1);
}
else
return Character.getNumericValue(number.charAt(0));

RESULT:
(i)
(ii)

Learning outcomes (What I have learnt):


1. Learnt about N-queen problem.
2. Learnt about Finding sum of digits problem.
3. Learnt about ways of solving above questions.

You might also like