Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had to write an n-queens solver in Java for a class a couple years ago in college. I'm not sure what type of source code you are looking for, but if it would be of any help here's the code from that program. It's not really optimized in any way, just a pretty simple recursive algorithm. I wrote this my first semester of college, so excuse the beginner mistakes:-) I took most of the block comments out, but hopefully you can make sense of it with the ones that I left in. If you want some professional source code than maybe do a Google search? I know that Wikipedia had some decent articles and pseudo-code. Hope this helps!</p> <pre><code>import java.util.Scanner; public class QueensSolver { public static void main(String args[]) { System.out.println("Please enter the size of the chessboard: "); Scanner stdin = new Scanner(System.in); int sizeOfBoard = stdin.nextInt(); //Create the board to the given size. char[][] board = new char[sizeOfBoard][sizeOfBoard]; //fill board with hyphens for(int row=0; row&lt;sizeOfBoard; row++) { for(int col=0; col&lt;sizeOfBoard; col++) { board[row][col] = '-'; } } //Call the method which attempts to solve the problem if(solve(sizeOfBoard, board, 0)) { //if true, we know that the board array contains a solution printBoard(sizeOfBoard, board); } else { //if false, we know that no solutions exist on this size of board System.out.println("No solutions are possible with this board size."); } } public static boolean solve(int sizeOfBoard, char[][] board, int row) { //If all rows have been filled, we have a solution if(row&gt;=sizeOfBoard) { return true; } //For each column(space) on the given row for(int col=0; col&lt;sizeOfBoard; col++) { //If placing a queen in this column(space) does not cause a conflict if(!checkConflict(sizeOfBoard, board, row, col)) { //place a queen here board[row][col]='Q'; //then call this same function on the next row boolean success = solve(sizeOfBoard, board, row+1); //if every function in this recursive path returns true if(success) { //then we return true also return true; } //If there is no possible solution with this queen placement, //then we replace the queen with an empty and attempt //to place a queen in the next column(space). else { board[row][col]='-'; } } } return false; } public static boolean checkConflict(int sizeOfBoard, char[][] board, int rowCheck, int colCheck) { //Check for queens placed directly above this position for(int row = rowCheck-1; row&gt;=0; row--) { if(board[row][colCheck]=='Q') { return true; } } //Check for queens placed on the diagonal //above and to the right of this position int col = colCheck+1; for(int row = rowCheck-1; row&gt;=0; row--) { if(col&gt;=sizeOfBoard) { break; } if(board[row][col]=='Q') { return true; } col++; } //Check for queens placed on the diagonal //above and to the left of this position col = colCheck-1; for(int row = rowCheck-1; row&gt;=0; row--) { if(col&lt;0) { break; } if(board[row][col]=='Q') { return true; } col--; } //We know that no conflicts are caused by this position, so return false return false; } public static void printBoard(int sizeOfBoard, char[][] board) { for(int row=0; row&lt;sizeOfBoard; row++) { for(int col=0; col&lt;sizeOfBoard; col++) { System.out.print(board[row][col]); } System.out.print("\n"); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload