Question

In: Computer Science

Write a JAVA pogram for the following scenario. Given an n × n × n cube...

Write a JAVA pogram for the following scenario. Given an n × n × n cube containing n3 cells, we are to place n queens in the cube so that no two queens challenge each other (so that no two queens are in the same row, column, or diagonal). In JAVA, implement it on your system to solve problem instances in which n = 4 and n = 8.

Solutions

Expert Solution

package optional1;
import java.util.Scanner;
public class Main {
  

// print the final solution matrix
static void printSolution(int board[][],int N)
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
System.out.print(" " + board[i][j]
+ " ");
System.out.println();
}
}

// function to check whether the position is safe or not
static boolean isSafe(int board[][], int row, int col,int N)
{
int i, j;
for (i = 0; i < col; i++)
if (board[row][i] == 1)
return false;

  
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j] == 1)
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j] == 1)
return false;
   return true;
}

// The function that solves the problem using backtracking
public static boolean solveNQueen(int board[][], int col, int N)
{
if (col >= N)
return true;

for (int i = 0; i < N; i++) {
//if it is safe to place the queen at position i,col -> place it
if (isSafe(board, i, col,N)) {
board[i][col] = 1;

if (solveNQueen(board, col + 1,N))
return true;

//backtrack if the above condition is false
board[i][col] = 0;
}
}
return false;
}
   public static void main(String args[])
{
int N;

Scanner in=new Scanner(System.in);
//System.out.print()
System.out.println("Enter the value of N");
N=in.nextInt();
  
int board[][] = new int[N][N];
for (int i = 0; i <N; i++)
for (int j = 0; j < N; j++)
board[i][j] = 0;
if (!solveNQueen(board, 0,N)) {
System.out.print("Solution does not exist");
return;
}
printSolution(board,N);
}
  }

OUTPUT COMPILED:


Related Solutions

Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
Write one Java program and satisfy the following requirements: Write a method called cube that accepts...
Write one Java program and satisfy the following requirements: Write a method called cube that accepts one integer parameter and returns that value raised to the third power. Write a method called randomNumber that returns a random floating-point number in the range of [-20.0, 50.0). (hints: use Random class in the method) Write a method called findMax that accepts three floating-point number as parameters and returns the largest one.(hints: use conditional statement in the method) Overload findMax that accepts one...
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
Write a java program calls the following methods: a. printStars(): Takes an int (n) as parameter...
Write a java program calls the following methods: a. printStars(): Takes an int (n) as parameter and prints n stars (*) using for loop. ex. 6 ******
Write a Java program for RSA encryption that has the following inputs and outputs: Given a...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
Given the following code below: public class Calculation { //method that returns cube of the given...
Given the following code below: public class Calculation { //method that returns cube of the given number public int findMax(int arr[]){    int max=0;    for(int i=1;i<arr.length;i++){        if(max<arr[i]){           max=arr[i]; }     }     return max;   } //method that returns cube of the given number   public static int cube(int n){        return n*n*n;     }   } (5 points) Define the class CalculationTest a subclass of TestCase (10 points) Define a setUp() method (20 points) Define a test method testfindMax that exercises Calculation.findMax() in Calculation class (5 points)Define...
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
2. Consider an n-cube, where n = 3. Is it possible to draw this on a...
2. Consider an n-cube, where n = 3. Is it possible to draw this on a two dimensional plane where the lines do not cross? If it is possible, draw it. If not, prove that it isn't possible.
Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 *...
Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 * 2 * 1 For example, 5! = 5 * 4 * 3 * 2 * 1 = 120 Write a function called factorial that takes as input an integer. Your function should verify that the input is positive (i.e. it is greater than 0). Then, it should compute the value of the factorial using a for loop and return the value. In main, display...
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT