daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //-----------------------------------------------------------------------------
  2. // NQueens.java
  3. // Solves the n-queens problem
  4. //-----------------------------------------------------------------------------
  5. import java.io.*;
  6. import java.util.Scanner;
  7.  
  8. class NQueens{
  9.    public void main(String[] args) throws IOException{
  10.       if(args.length < 2){
  11.        System.out.println("Usage: java –jar NQueens.jar <input file> <output file>");
  12.        System.exit(1);
  13.      }
  14.       // open files
  15.       Scanner in = new Scanner(new File(args[0]));
  16.       PrintWriter out = new PrintWriter(new FileWriter(args[1]));
  17.  
  18.       String[] token;
  19.  
  20.       while( in.hasNextLine() ){
  21.         // trim leading and trailing spaces, then add one trailing space so
  22.         // split works on blank lines
  23.         String line = in.nextLine().trim() + " ";
  24.  
  25.         // split line around white space
  26.         token = line.split("\\s+");
  27.         //if we dont have enough arguments to work from, cancel running.  
  28.         if(token.length%3 != 0){
  29.          System.out.println("Please input a parameter divisible by three.");
  30.          System.exit(1);
  31.         }
  32.       }
  33.       //converts token into an int array
  34.       int[] intoken = new int[token.length];
  35.       for(int i = 0; i < 3; i++) {
  36.         intoken[i] = Integer.parseInt(token[i]);
  37.       }
  38.  
  39.       //determines how many times we're going to solve NQueens
  40.       int val = intoken.length/3;
  41.       for (int j = 0; j <= val;j++){}
  42.       Queen[] solution = new Queen[intoken[0]]; //creates the solution array of length given
  43.  
  44.       //fills nQueens with 00s
  45.       for (int i=0;i<solution.length;i++){
  46.         solution[i] = new Queen(0,0);
  47.        }
  48.  
  49.       //replaces the 0,0 with the column and row given
  50.       solution[intoken[1]] = Queen(intoken[1],intoken[2]);
  51.       int r = 0;
  52.        if (nQueens(solution,r) == true) {
  53.            String sol;
  54.            for (int i = 0; i < solution.length;i++) {
  55.               sol = "" + solution[i] + " ";
  56.            }
  57.            System.out.print(sol);
  58.         }
  59.         if (nQueens(solution,c,r) == false)
  60.           System.out.print("Error");
  61.    }
  62.  
  63.    boolean nQueens(Queen[] s, int r){ //takes in array of queens
  64.       for (int i = 0; i < s.length){ //iteration to decide which column we're in
  65.          if (i == s.length) //if we've reached the end, we return that this is capable
  66.             return true;
  67.           if (r == s.length) //if we've completed a column without being able to place a queen, return false
  68.             return false;
  69.         //  if (s[i] != (0,0))
  70.          s[i] = Queen(i,r); //creates a new queen in the solutions array
  71.          for (int i = 0;i < solution.length;i++){
  72.           for (int j = 0;j < i;j++){ //ask about this logic
  73.             if solution[i].isAttacking(solution[j]){
  74.               r++;
  75.               nQueens(s,r);
  76.             }
  77.           }
  78.          }
  79.          r = 0;
  80.          i++;
  81.          nQueens(s,r);
  82.      }
  83.   }  
  84. }
  85.  
  86.  
  87. class Queen{
  88.    int col;
  89.    int row;
  90.    public void Queen(int c,int r){ //constructor
  91.       col = c;
  92.       row = r;
  93.    }
  94.    boolean isAttacking(Queen q) {
  95.       if (col==q.col || row == q.row) //horizontal and vertical check
  96.          return true;
  97.       if ((q.col + 1 == col && q.row + 1 ==row) || (q.col -1 == col && q.row -1 == row)) //diagonal check WIP
  98.          return false;
  99.    }
  100. }
RAW Paste Data
Top