Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*; // import for in and out files
- import java.util.Scanner; // scanner to input files
- import java.util.*;
- public class NQueens{
- public static int qrow; // row of chessboard
- public static int cols; // column of chessboard
- public static int[][] board; // boardsize
- public static PrintWriter out; // creates files
- public static boolean freeQ(int col, int row, int[][] board){ // row col size check. {
- for (int i = 0; i < board.length; i++){
- if (board[i][col] == 1){
- return false;
- }
- }
- for (int i = 0; i < board.length; i++){
- for (int j = 0; j < board.length; j++){ // make sure they dont line up with each other.
- if (col - row == i - j && board[j][i] == 1){
- return false; // if statement is true return queens as false
- }
- if (row + col == i + j && board[j][i] == 1){
- return false;
- }
- }
- }
- return true;
- } // diagonalzation
- public static boolean rQueens(int nq, int n){ // recursive part of the NQueens
- if (nq == n){
- return true;
- }
- if (nq == qrow){
- nq += 1;
- }
- for (int col = 0; col < n; col++){
- if (freeQ(col, nq, board) == true){ // compares all queens and has a backtrack. Make sure its true so it can backtrack correctly
- board[nq][col] = 1;
- if (rQueens(nq + 1, n) == true){
- return true;
- }
- board[nq][col] = 0;
- }
- }
- return false;
- }
- public static void main(String[] args) throws IOException{ // IO exception for the input and output files.
- // check number of command line arguments is at least 2
- if(args.length < 2){
- System.out.println("Usage: java βjar NQueens.jar <input file> <output file>"); // max of two files only
- System.exit(1);
- }
- // open files
- Scanner in = new Scanner(new File(args[0])); // put in file 1 aka test-input.txt
- PrintWriter out = new PrintWriter(new FileWriter(args[1])); // creates file 2 aka test-output.txt
- // read lines from in, extract and print tokens from each line
- while( in.hasNextLine() ){
- String line = in.nextLine(); // reads numbers in the input file. Splits each of the numbers up
- String[] token = line.split("\\s+");
- int boardsize = Integer.parseInt(token[0]); // each of the number arguments 0 , 1 , 2 = first num , sec num , third num.
- qrow = Integer.parseInt(token[1]);
- int qcols = Integer.parseInt(token[2]);
- qrow--; // de increments
- qcols--;
- board = new int[boardsize][boardsize]; // col and row for the board
- for (int i = 0; i < boardsize; i++){
- for (int j = 0; j < boardsize; j++){
- board[i][j] = 0;
- }
- }
- board[qrow][qcols] = 1;
- if (rQueens(0, boardsize) == true){ // everything under this is like the solution of the nQueens problem.
- for (int i = 0; i < boardsize; i++){
- for (int j = 0; j < boardsize; j++){
- if (board[i][j] == 1){
- out.print(Integer.toString(i+1) + " " + Integer.toString(j+1));
- out.println();
- }
- }
- }
- }
- else{
- out.println("No Solution");
- }
- }
- // close files
- in.close();
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement