Question: 1 Problem description Main objective: Solve the n queens problems. You have to place n queens on an n n chessboard such that no two
1 Problem description 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 queen in this position. Format: You should provide a Makefile. On running make, it should create NQueens.jar. You should run the jar with command line arguments described below. (All arguments are positive integers.) The first argument is n, the chessboard size. The second argument is the column where the input queen is present, and the third argument is the row of the input queen. For example, we would call the command java -jar NQueens.jar 7 3 2. This means we have a 7 7 chessboard, with one queen placed at (3, 2). We wish to place 6 more queens without any attacking the other. Output: On running the command, the following should be printed in solution.txt. If there is no solution to the problem, print No solution (with a newline at the end) If there is a solution, print the position of each queen in a separate line. 1 Examples: Consider java -jar NQueens.jar 7 3 2. The file solution.txt could contain:
3 2
1 1
2 5
4 6
5 3
6 7
7 4
Observe how 3 2 is part of the solution, since we started with a queen there. The solution is not unique, so your code might find some other placement. On the other hand java -jar NQueens.jar 4 1 1 (a 44 chessboard with a queen at (1, 1)) has no solution. So solution.txt should have: No solution Helper code: On the HW website, you will find a java file that prints out your solution on chessboard (with queen positions) on to your console. This is extremely helpful in checking if your solution is correct. You can run this as follows: Compile and execute the java code on terminal directly using the commands below: javac PrintSolutionHelper.java java PrintSolutionHelper solution file is the solution.txt file that your NQueens.jar generates. board size is the size of the board you input to NQueens.jar Example commands and outputs: On the HW website, you will find a file Examples.zip. Download and unzip it. You will find a file inputs.txt. This has a list of possible commands. For each command, you will also see an example solution. For the first line in inputs.txt, the corresponding solution is in the file solution1.txt. (So on and so forth for the other lines.) Your solution may be different from what is given, since there can be numerous solutions. This is just to give you an example output.
And this is what I wrote, I need help to finish my code:
import java.util.Scanner; import java.util.* import java.io.* class NQueens { public static void main(String args[]){ int number = Integer.parseInt(args[0]); int number1 = Integer.parseInt(args[1]); int number2 = Integer.parseInt(args[2]);
FileWriter fw = new FileWriter("solution.txt"); fw.write(" "+number+" "+number1+" "+number2);
int[] Qcolum; int[] Qrow; static boolean check(int[][] Qcolum, int y) { for (int i = 0;i < Qcolum.length; i++){ if (NotAttacking(number,i,y)) { Qcolum[i][y]==true; if (check(Qcolum, y+1)) { return true; } Qcolum[i][y]==false; }
} }
static boolean NotAttacking(int[], int x, int y){ int j = 0; int l=0; for (int i = 0; i < Qcolum.length; i++){ if (Qcolum[i][y] == true) return false; //are ith & jth queen in same colum if (Qcolum[x][i] == true) return false; //are ith & jth queen in same row if(Qcolum[i][j]==true) return false; j++; if (Qcolum[i][l]==true) return false; l--; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
