回溯法求八皇后问题

package javaapplication5;

/**
*
* @author Administrator
*/
import java.util.*;
public class Queens {
// squares per row or column
public static final int BOARD_SIZE = 8;

// used to indicate an empty square
public static final int EMPTY = 0;

// used to indicate square contains a queen
public static final int QUEEN = 1;

private int board[][]; // chess board
public Queens() {
// -------------------------------------------------
// Constructor: Creates an empty square board.
// -------------------------------------------------
board = new int[BOARD_SIZE][BOARD_SIZE];
} // end constructor

public void clearBoard() {
// -------------------------------------------------
// Clears the board.
// Precondition: None.
// Postcondition: Sets all squares to EMPTY.
// -------------------------------------------------
// To be implemented in Programming Problem 1
int i,j;
for(i=0;i<BOARD_SIZE;i++)
{
for(j=0;j<BOARD_SIZE;j++)
board[i][j] = EMPTY;
}

} // end clearBoard

public void displayBoard() {
// -------------------------------------------------
// Displays the board.
// Precondition: None.
// Postcondition: Board is written to standard
// output; zero is an EMPTY square, one is a square
// containing a queen (QUEEN).
// -------------------------------------------------
// To be implemented in Programming Problem 1
int i,j;
for(i=0;i<BOARD_SIZE;i++)
{
for(j=0;j<BOARD_SIZE;j++)
System.out.print( "\t"+board[i][j]);
System.out.println("\n");
}
} // end displayBoard

public boolean placeQueens(int column) {
// -------------------------------------------------
// Places queens in columns of the board beginning
// at the column specified.
// Precondition: Queens are placed correctly in
// columns 1 through column-1.
// Postcondition: If a solution is found, each
// column of the board contains one queen and method
// returns true; otherwise, returns false (no
// solution exists for a queen anywhere in column
// specified).
// -------------------------------------------------
if (column > BOARD_SIZE) {
return true; // base case
}
else {
boolean queenPlaced = false;
int row = 1; // number of square in column

while ( !queenPlaced && (row <= BOARD_SIZE) ) {
// if square can be attacked
if (isUnderAttack(row, column)) {
++row; // consider next square in column
} // end if
else { // place queen and consider next column
setQueen(row, column);
queenPlaced = placeQueens(column+1);
// if no queen is possible in next column,
if (!queenPlaced) {
// backtrack: remove queen placed earlier
// and try next square in column
removeQueen(row, column);
++row;
} // end if
} // end if
} // end while
return queenPlaced;
} // end if
} // end placeQueens

private void setQueen(int row, int column) {
// --------------------------------------------------
// Sets a queen at square indicated by row and
// column.
// Precondition: None.
// Postcondition: Sets the square on the board in a
// given row and column to QUEEN.
// --------------------------------------------------
// To be implemented in Programming Problem 1
board[index(row)][index(column)] = QUEEN;
} // end setQueen

private void removeQueen(int row, int column) {
// --------------------------------------------------
// Removes a queen at square indicated by row and
// column.
// Precondition: None.
// Postcondition: Sets the square on the board in a
// given row and column to EMPTY.
// --------------------------------------------------
// To be implemented in Programming Problem 1
board[index(row)][index(column)] = EMPTY;
} // end removeQueen

private boolean isUnderAttack(int row, int column) {
// --------------------------------------------------
// Determines whether the square on the board at a
// given row and column is under attack by any queens
// in the columns 1 through column-1.
// Precondition: Each column between 1 and column-1
// has a queen placed in a square at a specific row.
// None of these queens can be attacked by any other
// queen.
// Postcondition: If the designated square is under
// attack, returns true; otherwise, returns false.
// --------------------------------------------------
// To be implemented in Programming Problem 1
int r,c;
for(c=0;c<index(column);c++) //只对前column-1列检测是否被攻击
{
for(r=0;r<BOARD_SIZE;r++)
{
if(r==index(row)&&board[r][c]==QUEEN) return true;//同行检测
if((Math.abs(r-index(row))==Math.abs(c-index(column)))&&(board[r][c]==QUEEN)) return true; //对角线检测
}
}
return false;
} // end isUnderAttack

private int index(int number) {
// --------------------------------------------------
// Returns the array index that corresponds to
// a row or column number.
// Precondition: 1 <= number <= BOARD_SIZE.
// Postcondition: Returns adjusted index value.
// --------------------------------------------------
// To be implemented in Programming Problem 1
if((number >=1) &&( number <= BOARD_SIZE))
return number-1;
else
return 0;//Here,method should throws exception. ??
} // end index

public static void main(String[] args)
{
System.out.println("Queens Problem");
Queens q = new Queens();
if(q.placeQueens(1))
q.displayBoard();
else
System.out.println("No solution.");

}
} // end Queens
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值