CS141 Lab 

Take Home Assignment 3


N-Queen Problem



Instructor: Sitanshu Kumar

Due:  One hour before your scheduled lab of week May 12 ~ May 16



Assignment:

Here, you are required to write a program of N-queen problem. Suppose the size of chess board is N*N and we have N queens.

In chess a queen can move in any direction, either along rows, columns,or diagonals. If it meets another piece then it is able to take it. For this reason we say that a queen "attacks" a piece if it is on the same column or diagonal. The N-Queens problem is to place N queens on a chessboard so that no queen attacks any other.

If N=8, it is the "Eight Queens Problem".  It requires to place 8 queens on a standard chessboard so that no queen can attack any other queen. Two solutions are shown below.

pic1  pic2
Two solutions to the Eight Queens Problem


Requirement:

  1. Your program can input N from the command line
  2. Use recursive method to solve this problem
  3. Output all the solutions for N-queen problem
  4. Output the total number of solutions


Sample Output:

$ main 6   // (input N=6)
No.1
. Q . . . .
. . . Q . .
. . . . . Q
Q . . . . .
. . Q . . .
. . . . Q .

No.2
. . Q . . .
. . . . . Q
. Q . . . .
. . . . Q .
Q . . . . .
. . . Q . .

No.3
. . . Q . .
Q . . . . .
. . . . Q .
. Q . . . .
. . . . . Q
. . Q . . .

No.4
. . . . Q .
. . Q . . .
Q . . . . .
. . . . . Q
. . . Q . .
. Q . . . .

There are totally 4 solutions.





Attention:
    1. Put all the files you want to turn-in in one folder
    2. Goto:  https://www.cs.ucr.edu/ 
    3. Click "student", login using your account name and password
    4. Click "WWWTurnin"
    5. Choose the class section: "cs141"
    6. Specify you turn-in folder name
    7. Turn in