2016-02-10 NQueens

posted Feb 11, 2016, 8:38 AM by Samuel Konstantinovich   [ updated Feb 11, 2016, 8:38 AM ]
Your hw02 is to complete the nqueens problem. 
I gave you a base file with the requirements on my marge directory because of internet issues, but I am copying it here for you.

The three public methods that matter:
QueenBoard(int)
solve()
printSolution()




public class QueenBoard{
    private int[][]board;
    
    public QueenBoard(int size){
	board = new int[size][size];
    }

    /**
     *precondition: board is filled with 0's only.
     *postcondition: 
     *-return false, and board is still filled
     *with 0's for a board that cannot be solved.
     *-return true, and board is filled with the 
     *final configuration of the board after adding 
     *all n queens. Uses solveH
     */
    public boolean solve()
    {
	return solveH(0);
    }

    /**
     *Helper method for solve. 
     */
    private boolean solveH(int col){
	return false;
    }

    /**Print the board like toString, except
     *all negative numbers, and 0's are replaced with '_'
     *and all 1's are replaced with 'Q' 
     */
public void printSolution(){
    /*Since there are only 1 character outputs, 
     *space separation is better than tab.
         */    } /********Do Not Edit Below This Line**********************************/ private boolean addQueen(int row, int col){ if(board[row][col] != 0){ return false; } board[row][col] = 1; int offset = 1; while(col+offset < board[row].length){ board[row][col+offset]--; if(row - offset >= 0){ board[row-offset][col+offset]--; } if(row + offset < board.length){ board[row+offset][col+offset]--; } offset++; } return true; } private boolean removeQueen(int row, int col){ if(board[row][col] != 1){ return false; } board[row][col] = 0; int offset = 1; while(col+offset < board[row].length){ board[row][col+offset]++; if(row - offset >= 0){ board[row-offset][col+offset]++; } if(row + offset < board.length){ board[row+offset][col+offset]++; } offset++; } return true; } public String toString(){ String ans = ""; for(int r = 0; r < board.length; r++){ for(int c = 0; c < board[0].length; c++){ ans+= board[r][c]+" "; } ans+="\n"; } return ans; } }
Comments