Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------------------------------------------
- // NQueens.java
- // Solves the n-queens problem
- //-----------------------------------------------------------------------------
- import java.io.*;
- import java.util.Scanner;
- class NQueens{
- public void main(String[] args) throws IOException{
- if(args.length < 2){
- System.out.println("Usage: java –jar NQueens.jar <input file> <output file>");
- System.exit(1);
- }
- // open files
- Scanner in = new Scanner(new File(args[0]));
- PrintWriter out = new PrintWriter(new FileWriter(args[1]));
- String[] token;
- while( in.hasNextLine() ){
- // trim leading and trailing spaces, then add one trailing space so
- // split works on blank lines
- String line = in.nextLine().trim() + " ";
- // split line around white space
- token = line.split("\\s+");
- //if we dont have enough arguments to work from, cancel running.
- if(token.length%3 != 0){
- System.out.println("Please input a parameter divisible by three.");
- System.exit(1);
- }
- }
- //converts token into an int array
- int[] intoken = new int[token.length];
- for(int i = 0; i < 3; i++) {
- intoken[i] = Integer.parseInt(token[i]);
- }
- //determines how many times we're going to solve NQueens
- int val = intoken.length/3;
- for (int j = 0; j <= val;j++){}
- Queen[] solution = new Queen[intoken[0]]; //creates the solution array of length given
- //fills nQueens with 00s
- for (int i=0;i<solution.length;i++){
- solution[i] = new Queen(0,0);
- }
- //replaces the 0,0 with the column and row given
- solution[intoken[1]] = Queen(intoken[1],intoken[2]);
- int r = 0;
- if (nQueens(solution,r) == true) {
- String sol;
- for (int i = 0; i < solution.length;i++) {
- sol = "" + solution[i] + " ";
- }
- System.out.print(sol);
- }
- if (nQueens(solution,c,r) == false)
- System.out.print("Error");
- }
- boolean nQueens(Queen[] s, int r){ //takes in array of queens
- for (int i = 0; i < s.length){ //iteration to decide which column we're in
- if (i == s.length) //if we've reached the end, we return that this is capable
- return true;
- if (r == s.length) //if we've completed a column without being able to place a queen, return false
- return false;
- // if (s[i] != (0,0))
- s[i] = Queen(i,r); //creates a new queen in the solutions array
- for (int i = 0;i < solution.length;i++){
- for (int j = 0;j < i;j++){ //ask about this logic
- if solution[i].isAttacking(solution[j]){
- r++;
- nQueens(s,r);
- }
- }
- }
- r = 0;
- i++;
- nQueens(s,r);
- }
- }
- }
- class Queen{
- int col;
- int row;
- public void Queen(int c,int r){ //constructor
- col = c;
- row = r;
- }
- boolean isAttacking(Queen q) {
- if (col==q.col || row == q.row) //horizontal and vertical check
- return true;
- if ((q.col + 1 == col && q.row + 1 ==row) || (q.col -1 == col && q.row -1 == row)) //diagonal check WIP
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement