Question

Main objective: Solve the n queens problems. You have to place n queens on an n...

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.
Specifics: You should provide a Makefile. On running make, it should create
“NQueens.jar”. You should run the jar with two command line arguments: the first is
an input file, the second is the output file. For example, we would call the command:
java -jar NQueens.jar in.txt solution.txt
The file in.txt will have inputs to the main program (as described below), and
the file solution.txt will have the desired solution.
Each line of the input file corresponds to a different instance. Each line of the
input file will have three integers: the chessboard size, the column where the input
queen is placed, and the row where the input queen is placed. For example, the file
may look like:
7 3 2
4 1 1
The first line means we have a 7 × 7 chessboard, with one queen placed at (3, 2).
We wish to place 6 more queens so that none of the 7 queens attack any other. The
second line means we have a 4 × 4 chessboard, with one queen at (1, 1). We wish to
place 3 more queens without any attacks. So on and so forth.

Output: On running the command, the following should be printed in the output
file. For each line of the input file, there is a line in the output file with the placement of Queens

• 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 as <column> <space>
<row> <space> followed by the position for the next queen. The positions
should be in increasing order of column, so first column first, second column
second, etc. Please follow this format exactly, so that the checking script works
for you. The last character on the line will be a space, then followed by a
newline. This format should make your code easier to write.
For example, the output for the input described above could be:
1 1 2 5 3 2 4 6 5 3 6 7 7 4
No solution

Observe how “3 2” is part of the first 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, a 4 × 4 chessboard with a queen at (1, 1) has no solution.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package file;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Queens {

  
  
  
   StringBuilder sb=new StringBuilder();
     public String[] flipLines(Scanner s){
  
       String[] numbers=null;
         try
         {
    s = new Scanner(new File("in.txt"));
    while(s.hasNextLine()){
    String line = s.nextLine();
    System.out.println(line);
    Scanner lineScanner = new Scanner(line);
  
    numbers = line.split(" ");
  
    }
  
    //exceptions
    } catch (FileNotFoundException ex){
    System.out.println("This file does not exist");
    }
         return numbers;
    }
  
  
  
  
  
  
  
       /***************************************************************************
        * Return true if queen placement q[n] does not conflict with
        * other queens q[0] through q[n-1]
        */
        public static boolean isConsistent(int[] q, int n) {
            for (int i = 0; i < n; i++) {
                if (q[i] == q[n])             return false;   // same column
                if ((q[i] - q[n]) == (n - i)) return false;   // same major diagonal
                if ((q[n] - q[i]) == (n - i)) return false;   // same minor diagonal
            }
            return true;
        }

       /***************************************************************************
        * Prints n-by-n placement of queens from permutation q in ASCII.
        ***************************************************************************/
        public static void printQueens(int[] q) {
            int n = q.length;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (q[i] == j) System.out.print("Q ");
                    else      System.out.print("* ");    
                }
                System.out.println();
            }
            System.out.println();
        }


       /***************************************************************************
        * Try all permutations using backtracking
        ***************************************************************************/
        public static void enumerate(int n) {
            int[] a = new int[n];
            enumerate(a, 0);
        }

        public static void enumerate(int[] q, int k) {
            int n = q.length;
            if (k == n) printQueens(q);
            else {
                for (int i = 0; i < n; i++) {
                    q[k] = i;
                    if (isConsistent(q, k)) enumerate(q, k+1);
                }
            }
        }


        public static void main(String[] args) {
        
          
           Queens q=new Queens();
           String[] sarray=q.flipLines(new Scanner(System.in));
          
        //   Scanner sc=new Scanner(System.in);
           int n=Integer.parseInt(sarray[0]);
       
            enumerate(n);
        }

   }

/*output:-

4 1 1
* Q * *
* * * Q
Q * * *
* * Q *

* * Q *
Q * * *
* * * Q
* Q * *


*/

Add a comment
Know the answer?
Add Answer to:
Main objective: Solve the n queens problems. You have to place n queens on an n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard...

    please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard (8 x 8 board) such that no queen is "attacking" another. Queens in chess can move vertically, horizontally, or diagonally. How you solve this problem is entirely up to you. You may choose to write a recursive program or an iterative (i.e., non-recursive) program. You will not be penalized/rewarded for choosing one method or another. Do what is easiest for you. 3.1. Output Below...

  • Assignment 2 In this assignment, you will write two short programs to solve problems using recursion....

    Assignment 2 In this assignment, you will write two short programs to solve problems using recursion. 1. Initial Setup Log in to Unix. Run the setup script for Assignment 2 by typing: setup 2 2. Towers of Hanoi Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by...

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    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...

  • WRITE A JAVA PROGRAM using STACKS and backtracing to solves the N Queens Problem . The...

    WRITE A JAVA PROGRAM using STACKS and backtracing to solves the N Queens Problem . The program takes the user's input integer for N and prints out all the solutions for N . The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. For example, the following is the output for 4 entered for userinput. Output for 4 Queens : 1- * * Q * Q *...

  • ================Data Structures C++=============== – Implement the Eight Queens Problem This is a...

    ================Data Structures C++=============== – Implement the Eight Queens Problem This is an object oriented program. This is #1 on page 187 of your book with ONE difference, I’m requiring you add a client program to test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem.” On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of...

  • in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the...

    in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide missing logic for the class Queens that will enable it to create a two-dimensional array that...

  • Complete the program that solves the Eight Queens problem. The program’s output should look similar to:...

    Complete the program that solves the Eight Queens problem. The program’s output should look similar to: |1|0|0|0|0|0|0|0| |0|0|0|0|0|0|1|0| |0|0|0|0|1|0|0|0| |0|0|0|0|0|0|0|1| |0|1|0|0|0|0|0|0| |0|0|0|1|0|0|0|0| |0|0|0|0|0|1|0|0| |0|0|1|0|0|0|0|0| Use the Queens class given. In your implementation of the Queens class, complete the body of all methods marked as “To be implemented in Programming Problem 1.” Do not change any of the global variable declarations, constructor or placeQueens methods. Here is what I have so far with notes of what is needed. public class Queens...

  • 110Marks Question No. 4 Eight queens problem: place 8 queens on a chess board so that no two queens attack each other. -state: locations of 0 to 8 queens (with no two queens attacking each other) goa...

    110Marks Question No. 4 Eight queens problem: place 8 queens on a chess board so that no two queens attack each other. -state: locations of 0 to 8 queens (with no two queens attacking each other) goal test: 8 queens placed on the board (with none attacked) operator: place a queen in the left-most empty column such that it is not attacked by any other queen (and does not attack any other queen) path cost: 0 Depth first search: i....

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

  • Artificial Intelligence homework. Please answer correctly. 3. Assume you were asked to place 8 queens on...

    Artificial Intelligence homework. Please answer correctly. 3. Assume you were asked to place 8 queens on an 8x8 chessboard such that no two queens can attack each other (i.e. share the same row, column, or diagonal). Please introduce a solution to use genetic algorithm to solve the game. Please explain the encoding of the state for genetic algorithm (i.e., the representation of the chromosome), the fitness function, the cross over, and the mutation process. [1 pt].

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT