Question

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

Write a JAVA program for the following scenario. Given an n × n × n cube containing n3 cells. PLEASE NOTE THAT THIS BOARD HAS 3 DIMENSIONS. 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.

Homework Answers

Answer #1

/* Java program to solve N Queen Problem using
backtracking */
public class NQueenProblem {
   final int N = 4;

   /* A utility function to print solution */
   void printSolution(int board[][])
   {
       for (int i = 0; i < N; i++) {
           for (int j = 0; j < N; j++)
               System.out.print(" " + board[i][j]
                               + " ");
           System.out.println();
       }
   }

   /* A utility function to check if a queen can
   be placed on board[row][col]. Note that this
   function is called when "col" queens are already
   placeed in columns from 0 to col -1. So we need
   to check only left side for attacking queens */
   boolean isSafe(int board[][], int row, int col)
   {
       int i, j;

       /* Check this row on left side */
       for (i = 0; i < col; i++)
           if (board[row][i] == 1)
               return false;

       /* Check upper diagonal on left side */
       for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
           if (board[i][j] == 1)
               return false;

       /* Check lower diagonal on left side */
       for (i = row, j = col; j >= 0 && i < N; i++, j--)
           if (board[i][j] == 1)
               return false;

       return true;
   }

   /* A recursive utility function to solve N
   Queen problem */
   boolean solveNQUtil(int board[][], int col)
   {
       /* base case: If all queens are placed
       then return true */
       if (col >= N)
           return true;

       /* Consider this column and try placing
       this queen in all rows one by one */
       for (int i = 0; i < N; i++) {
           /* Check if the queen can be placed on
           board[i][col] */
           if (isSafe(board, i, col)) {
               /* Place this queen in board[i][col] */
               board[i][col] = 1;

               /* recur to place rest of the queens */
               if (solveNQUtil(board, col + 1) == true)
                   return true;

               /* If placing queen in board[i][col]
               doesn't lead to a solution then
               remove queen from board[i][col] */
               board[i][col] = 0; // BACKTRACK
           }
       }

       /* If the queen can not be placed in any row in
       this colum col, then return false */
       return false;
   }

   /* This function solves the N Queen problem using
   Backtracking. It mainly uses solveNQUtil () to
   solve the problem. It returns false if queens
   cannot be placed, otherwise, return true and
   prints placement of queens in the form of 1s.
   Please note that there may be more than one
   solutions, this function prints one of the
   feasible solutions.*/
   boolean solveNQ()
   {
       int board[][] = { { 0, 0, 0, 0 },
                       { 0, 0, 0, 0 },
                       { 0, 0, 0, 0 },
                       { 0, 0, 0, 0 } };

       if (solveNQUtil(board, 0) == false) {
           System.out.print("Solution does not exist");
           return false;
       }

       printSolution(board);
       return true;
   }

   // driver program to test above function
   public static void main(String args[])
   {
       NQueenProblem Queen = new NQueenProblem();
       Queen.solveNQ();
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write java program to print the following shape N lines. * * * * * *...
Write java program to print the following shape N lines. * * * * * * * * * * * * * * * * * * * * * Input: N Output: The printed shape
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Note: There is a white space in between the numbers. Java programming please answer ASAP before 2:30
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS...
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS = 5000 # generates a random n-queens board # representation: a list of length n the value at index i is # row that contains the ith queen; # exampe for 4-queens: [0,2,0,3] means that the queen in column 0 is # sitting in row 0, the queen in colum 1 is in row, the queen in column 2 # is in row 0,...
Solve the following using java Write a program that runs three threads, each thread randomizes a...
Solve the following using java Write a program that runs three threads, each thread randomizes a number between 1 and 100. The main thread waits for all the others to finish, calculates the maximum of the numbers, which were randomized, and prints it. Random number 1: 10 Random number 2: 38 Random number 3: 81 Max: 81 Note: You should create 3 threads in adition to the main thread. Also, you can use a single thread class and create 3...
Write Java program Lab42.java which takes as input two positive integers m and n and computes...
Write Java program Lab42.java which takes as input two positive integers m and n and computes their least common multiple by calling method lcm(m,n), which in turn calls recursive method gcd(m,n) computing the greatest common divisor of m and n. Recall, that lcm(m,n) can be defined as quotient of m * n and gcd(m,n).
Write a program in java that generates a two-column table showing Fahrenheit temperatures from -40F to...
Write a program in java that generates a two-column table showing Fahrenheit temperatures from -40F to 120F and their equivalent Celsius temperatures. Each line in the table should be 5 degrees F more than the previous one. Both the Fahrenheit and Celsius temperatures should be accurate to 1 decimal place. I am not able to use: import java.text.DecimalFormat; Any feedback would be helpful.
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional char array with three rows and three columns as the game board. Each element in the array should be initialized with an asterisk (*). The program should run a loop that: • Displays the contents of the board array. • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
Problem Statement: Write a Java program “HW6_lastname.java” that prints a program title and a menu with...
Problem Statement: Write a Java program “HW6_lastname.java” that prints a program title and a menu with four items. The user should then be prompted to make a selection from the menu and based on their selection the program will perform the selected procedure. The title and menu should be as the following: Student name: <your name should be printed> CIS 232 Introduction to Programming Programming Project 6 Due Date: October 23, 2020 Instructor: Dr. Lomako ******************************** 1.     Compute Angles                               *...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is -3 10...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT