NQueen2.java

~cpp 
import java.io.PrintStream;


public class NQueen2
{
	public static final char DEFAULT_BOARD_MARK = '.';
	public static final char DEFAULT_QUEEN_MARK = 'Q';
	public static final char DEFAULT_LINE_BREAK = '\n';
	
	
	private int size;
	private int [] board;
	private int numberOfAnswers;
	private boolean checkOne;
	private int hasAnswer;
	private char boardMark;
	private char queenMark;
	private char lineBreak;
	private PrintStream out;
	
	
	private NQueen2() {}
	public NQueen2( int size ) throws Exception
	{
		if( size < 1 ) throw new Exception( NQueen2.class.getName() + "- size must be greater than 0." );
		this.size = size;
		board = new int[size];
		numberOfAnswers = -1;
		checkOne = false;
		hasAnswer = -1;
		setDefaultOutputFormat();
	}
	
	
	public int getSize()
	{
		return size;
	}
	
	
	public void setDefaultOutputFormat()
	{
		boardMark = DEFAULT_BOARD_MARK;
		queenMark = DEFAULT_QUEEN_MARK;
		lineBreak = DEFAULT_LINE_BREAK;	
	}
	
	
	public void setOutputFormat( final char boardMark, final char queenMark, final char lineBreak )
	{
		this.boardMark = boardMark;
		this.queenMark = queenMark;
		this.lineBreak = lineBreak;
	}
	
	
	public int countAnswers()
	{
		return countAnswers( null );
	}
	public int countAnswers( final PrintStream out )
	{
		if( out != null || numberOfAnswers == -1 ) {
			this.out = out;
			setQueens();
			this.out = null;
		}
	
		return numberOfAnswers;
	}


	public boolean hasAnswers()
	{
		if( hasAnswer == -1 ) {
			boolean prevCheckOne = checkOne;
			checkOne = true;
			setQueens();
			checkOne = prevCheckOne;
			numberOfAnswers = -1;
		}
		if( hasAnswer == 1 ) return true;
		
		return false;
	}
	
	
	private void setQueens()
	{
		numberOfAnswers = 0;
		setQueenAt( 0 );
	}
	
	
	private void setQueenAt( int line )
	{
		if( line == size ) {
			if( isValidQueens() ) {
				hasAnswer = 1;
				numberOfAnswers ++;
				if( out != null ) printQueen();
			}
			return;
		}
		
		for( int xPos=0; xPos<size; xPos++ ) {
			if( checkOne && numberOfAnswers > 0 ) break;
			board[line] = xPos;
			setQueenAt( line + 1 );
		}
	}
	
	
	private boolean isValidQueens()
	{
		for( int i=0; i<size; i++ )
			if( ! checkRule( i, board[i] )) return false;
	
		return true;
	}
	
	
	private boolean checkRule( final int line, int xPos )
	{
		int rightDownWay = xPos - line;
		int leftDownWay = xPos + line;
		for( int i=0; i<size; i++ ) {
			if( line != i )
				if( board[i] == xPos || board[i] == rightDownWay || board[i] == leftDownWay ) return false;
			rightDownWay ++;
			leftDownWay --;
		}
		return true;
	}


	private void printQueen()
	{
		for( int i=0; i<size; i++ ) {
			for( int j=0; j<size; j++ ) {
				if( board[i] == j ) out.print( queenMark );
				else out.print( boardMark );
			}
			out.print( lineBreak );
		}
	}


	public static void main( String [] args )
	{
		int sizeOfBoard = 0;
		try {
			if( args.length != 1 ) throw new Exception();
			sizeOfBoard = Integer.parseInt( args[0] );
		}
		catch( Exception e ) {
			System.out.println( "Usage: java NQueen2 size" );
			System.exit( 1 );
		}
		
		
		try {
			NQueen2 nq = new NQueen2( sizeOfBoard );
			
			System.out.println( "has answers? " + nq.hasAnswers() );
			System.out.println( "number of answers: " + nq.countAnswers());
			System.out.println( "has answers? " + nq.hasAnswers() );
			
			System.out.println( "number of answers: " + nq.countAnswers( System.out ));
			nq.setOutputFormat( '-','q','\n' );
			System.out.println( "number of answers: " + nq.countAnswers( System.out ));
		}
		catch( Exception e ) {
			e.printStackTrace();
		}
	}
}
Retrieved from http://wiki.zeropage.org/wiki.php/EightQueenProblem/이선우2
last modified 2021-02-07 05:23:12