n-Queens Problems:
import java.util.Scanner;
public class QueensProblem {
public static void possibleChoices(int k) {
int[] choices = new int[k];
possibleChoices (choices, 0);
}
public static void possibleChoices(int[] queen, int
no) {
int n = queen.length;
if (no == n) display(queen);
else {
for (int i = 0; i < n; i++) {
queen[no] = i;
if (isConsistentApproach(queen, no))
possibleChoices(queen, no+1);
}
}
}
public static void display(int[] queen) {
int z = queen.length;
for (int k = 0; k < z; k++) {
for (int j = 0; j < z; j++) {
if (queen[k] == j) System.out.print("x ");
else System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
public static boolean isConsistentApproach(int[]
queen, int no) {
for (int k = 0; k < no; k++) {
if (queen[k] == queen[no]) return false; // same
column
if ((queen[k] - queen[no]) == (no - k)) return false;
// same major diagonal
if ((queen[no] - queen[k]) == (no - k)) return false;
// same minor diagonal
}
return true;
}
public static void main(String...
arguments) {
Scanner scan=new
Scanner(System.in);
System.out.println("enter the instance you want");
int
choice=scan.nextInt();
QueensProblem.possibleChoices(choice);
}
}
output:
enter the instance you want
5
x * * * *
* * x * *
* * * * x
* x * * *
* * * x *
x * * * *
* * * x *
* x * * *
* * * * x
* * x * *
* x * * *
* * * x *
x * * * *
* * x * *
* * * * x
* x * * *
* * * * x
* * x * *
x * * * *
* * * x *
* * x * *
x * * * *
* * * x *
* x * * *
* * * * x
* * x * *
* * * * x
* x * * *
* * * x *
x * * * *
* * * x *
x * * * *
* * x * *
* * * * x
* x * * *
* * * x *
* x * * *
* * * * x
* * x * *
x * * * *
* * * * x
* x * * *
* * * x *
x * * * *
* * x * *
* * * * x
* * x * *
x * * * *
* * * x *
* x * * *
enter the instance you want
6
* x * * * *
* * * x * *
* * * * * x
x * * * * *
* * x * * *
* * * * x *
* * x * * *
* * * * * x
* x * * * *
* * * * x *
x * * * * *
* * * x * *
* * * x * *
x * * * * *
* * * * x *
* x * * * *
* * * * * x
* * x * * *
* * * * x *
* * x * * *
x * * * * *
* * * * * x
* * * x * *
* x * * * *
enter the instance you want
7
x * * * * * *
* * x * * * *
* * * * x * *
* * * * * * x
* x * * * * *
* * * x * * *
* * * * * x *
x * * * * * *
* * * x * * *
* * * * * * x
* * x * * * *
* * * * * x *
* x * * * * *
* * * * x * *
x * * * * * *
* * * * x * *
* x * * * * *
* * * * * x *
* * x * * * *
* * * * * * x
* * * x * * *
x * * * * * *
* * * * * x *
* * * x * * *
* x * * * * *
* * * * * * x
* * * * x * *
* * x * * * *
* x * * * * *
* * * x * * *
x * * * * * *
* * * * * * x
* * * * x * *
* * x * * * *
* * * * * x *
* x * * * * *
* * * x * * *
* * * * * x *
x * * * * * *
* * x * * * *
* * * * x * *
* * * * * * x
* x * * * * *
* * * * x * *
x * * * * * *
* * * x * * *
* * * * * * x
* * x * * * *
* * * * * x *
* x * * * * *
* * * * x * *
* * x * * * *
x * * * * * *
* * * * * * x
* * * x * * *
* * * * * x *
* x * * * * *
* * * * x * *
* * * * * * x
* * * x * * *
x * * * * * *
* * x * * * *
* * * * * x *
* x * * * * *
* * * * * x *
* * x * * * *
* * * * * * x
* * * x * * *
x * * * * * *
* * * * x * *
* x * * * * *
* * * * * * x
* * * * x * *
* * x * * * *
x * * * * * *
* * * * * x *
* * * x * * *
* * x * * * *
x * * * * * *
* * * * * x *
* x * * * * *
* * * * x * *
* * * * * * x
* * * x * * *
* * x * * * *
x * * * * * *
* * * * * x *
* * * x * * *
* x * * * * *
* * * * * * x
* * * * x * *
* * x * * * *
* * * * x * *
* * * * * * x
* x * * * * *
* * * x * * *
* * * * * x *
x * * * * * *
* * x * * * *
* * * * * x *
* x * * * * *
* * * * x * *
x * * * * * *
* * * x * * *
* * * * * * x
* * x * * * *
* * * * * * xx
* x * * * * *
* * * x * * *
* * * * * x *
x * * * * * *
* * * * x * *
* * x * * * *
* * * * * * x
* * * x * * *
x * * * * * *
* * * * x * *
* x * * * * *
* * * * * x *
* * * x * * *
x * * * * * *
* * x * * * *
* * * * * x *
* x * * * * *
* * * * * * x
* * * * x * *
* * * x * * *
x * * * * * *
* * * * x * *
* x * * * * *
* * * * * x *
* * x * * * *
* * * * * * x
* * * x * * *
* x * * * * *
* * * * * * x
* * * * x * *
* * x * * * *
x * * * * * *
* * * * * x *
* * * x * * *
* * * * * x *
x * * * * * *
* * x * * * *
* * * * x * *
* * * * * * x
* x * * * * *
* * * x * * *
* * * * * * x
* * x * * * *
* * * * * x *
* x * * * * *
* * * * x * *
x * * * * * *
* * * x * * *
* * * * * * x
* * * * x * *
* x * * * * *
* * * * * x *
x * * * * * *
* * x * * * *
* * * * x * *
x * * * * * *
* * * x * * *
* * * * * * x
* * x * * * *
* * * * * x *
* x * * * * *
* * * * x * *
x * * * * * *
* * * * * x *
* * * x * * *
* x * * * * *
* * * * * * x
* * x * * * *
* * * * x * *
* x * * * * *
* * * * * x *
* * x * * * *
* * * * * * x
* * * x * * *
x * * * * * *
* * * * x * *
* * x * * * *
x * * * * * *
* * * * * x *
* * * x * * *
* x * * * * *
* * * * * * x
* * * * x * *
* * * * * * x
* x * * * * *
* * * x * * *
* * * * * x *
x * * * * * *
* * x * * * *
* * * * x * *
* * * * * * x
* x * * * * *
* * * * * x *
* * x * * * *
x * * * * * *
* * * x * * *
* * * * * x *
x * * * * * *
* * x * * * *
* * * * x * *
* * * * * * x
* x * * * * *
* * * x * * *
* * * * * x *
* x * * * * *
* * * * x * *
x * * * * * *
* * * x * * *
* * * * * * x
* * x * * * *
* * * * * x *
* * x * * * *
x * * * * * *
* * * x * * *
* * * * * * x
* * * * x * *
* x * * * * *
* * * * * x *
* * x * * * *
* * * * x * *
* * * * * * x
x * * * * * *
* * * x * * *
* x * * * * *
* * * * * x *
* * x * * * *
* * * * * * x
* * * x * * *
x * * * * * *
* * * * x * *
* x * * * * *
* * * * * x *
* * * x * * *
* x * * * * *
* * * * * * x
* * * * x * *
* * x * * * *
x * * * * * *
* * * * * x *
* * * x * * *
* * * * * * x
x * * * * * *
* * x * * * *
* * * * x * *
* x * * * * *
* * * * * * x
* x * * * * *
* * * x * * *
* * * * * x *
x * * * * * *
* * x * * * *
* * * * x * *
* * * * * * x
* * x * * * *
* * * * * x *
* x * * * * *
* * * * x * *
x * * * * * *
* * * x * * *
* * * * * * x
* * * x * * *
x * * * * * *
* * * * x * *
* x * * * * *
* * * * * x *
* * x * * * *
* * * * * * x
* * * * x * *
* * x * * * *
x * * * * * *
* * * * * x *
* * * x * * *
* x * * * * *
9. Suppose we have a solution to the n-Queens problem instance in which n 4. Can...
Main objective: Solve the n queens problems. You have to place n queens on an n × n chessboard such that no two attack each other. Important: the chessboard should be indexed starting from 1, in standard (x, y) coordinates. Thus, (4, 3) refers to the square in the 4th column and 3rd row. We have a slight twist in this assignment. We will take as input the position of one queen, and have to generate a solution with a...
Suppose you are given an array of n integers a1, a2, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for {5, 10, 9, 13, 12, 25, 19, 70} is 6 and one such sub-array is {5, 10, 13, 19, 70}. Note you may have multiple solutions for this case. Use dynamic programming to...
Suppose you are given an array of n integers ai, az, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for (5, 10, 9, 13, 12, 25, 19, 30) is 5 and one such sub-array is (5, 10, 13, 19, 303. Note you may have multiple solutions for this case. Use dynamic programming to...
Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing and combining steps take a time in Θ(n n). Write a recurrence equation for the running time T (n) , and solve the equation for T (n) 2. Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing...
Exercise 12.6 At each stage, one can either pay 1 and receive a coupon that is equally likely to be any of n types, or one can stop and receive a final reward of jr if one's current collection of coupons contains exactly j distinct types. Thus, for instance, if one stops after having previously obtained six coupons whose successive types were 2, 4, 2, 5, 4, 3, then one would have earned a net return of 4r -6. The...
Problem 2 We have seen that, if a matrix A is invertible, then we can express the unique solution of Az = b as I = A-15. Soon, we will introduce ideas that help us understand At = b better when A is not invertible. This problem is preparation for that! 11 301 1-3 -9 2 Let A = 2 6 0 1-2 -6 -5 (a) Solve the system A7 = 5. (b) What does the solution set of Az...
Show that, without backtracking, 155 nodes must be checked before the first solution to the n = 4 instance of the n-Queens problem is found (in contrast to the 27 nodes in Figure 1).Use mathmetical proof. 1,2 2.3 2.4 2, 1 2, 2 2,3 2,4 2, 2 3, 1 4,14,2)(4,3)4,4 4,14,2) 4, 3 1,2 2.3 2.4 2, 1 2, 2 2,3 2,4 2, 2 3, 1 4,14,2)(4,3)4,4 4,14,2) 4, 3
can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...
Suppose you are given an instance of the fractional knapsack problem in which all the items have the same weight. Show that you can solve the fractional knapsack problem in this case in O(n) time.
Problem 2.6.1. We have 7*6* 55 = 2310. Extend the technique of the Chinese remainder theorem to find the solutions in Z2310 of the equations. 2x => 3 X 564 5x 355 50 Hint: There will be 5 solutions because the last equation has 5 solutions. You may use the Mathematica command ExtendedGCD.