在制作程序为8皇后拼图找到1个解决方案时,我发现了一个问题。我正在使用一个树,其中每个节点都有一个代表游戏Board的矩阵。每个新节点都有一个免费的新女王在给定的行上定位,并找到解决方案我正在实现各种路径寻找算法如BFS,A *等,然后我将比较它们的性能。 我从根节点开始,在位置游戏[0] [0]上插入1个大号。
我正在实现的第一个算法是BFS,所以这个根节点进入队列,虽然队列不为空并且找不到解决方案(一个有8个皇后的节点),我将删除第一个队列的元素和这个节点我去第一个免费线并搜索一个空位以放置一个女王。对于每个自由位置(不受其他女王的攻击),我创建一个节点,插入一个女王并将其添加到队列中。
我有3个课程: GameBoard ,* 搜索 *这是我正在使用所有节点(GameBoard)和 NQueen ,我调用算法函数和初始化de Search树的主类; 这是我的BFS功能,我认为问题在于'而'。如果未验证'if'子句,则parentNode不会更改,但如果是,则< strong>'insertNewQueen'称为 parentNode 更改并“转入”生成的节点。
void BreadthFirstSearch() {
String search = "BREADTH"; /* to identify the algorithm I'm using when pushing elements to the queue*/
boolean end = false;
while(end == false && !queue.isEmpty()) {
GameBoard parentBoard = this.queue.removeFirst();
//parentBoard.printGameBoard();
//System.out.println();
if(parentBoard.nqueens == 8) {/*if a solution was found*/
end = true;
parentBoard.printGameBoard();
}
else {
int row = parentBoard.lastQueenRow + 1;
for(int column = 0; column < GameBoard.WIDTH; column++) {
System.out.println("ROW:" + row);
parentBoard.printGameBoard();
System.out.println();
if(parentBoard.isSafetoInsertQueen(row,column)) {
GameBoard newBoard = new GameBoard(parentBoard);
System.out.println("PIM");
insertNewQueen(newBoard, row, column, search);
}
}
end = true;
/*for(GameBoard board : queue) {
board.printGameBoard();
System.out.println();
}*/
}
}
}
这是GameBoard构造函数:
GameBoard(GameBoard parent) {
this.board = parent.board.clone(); /*matrix[8][8]
this.nqueens = parent.nqueens; /*number of queens in this gameBoard*/
this.parent = parent; /* the previous state*/
this.lastQueenRow = 0;/*line where the queen was inserted in this gameBoard*/
}
insertNewQueen 功能相当大。它没有以非常有效的方式完成,我打算在完成程序后优化和清理我的代码。基本上它收到一个GameBoard放一个女王给定的位置然后继续“攻击”相应的直线,列和对角线。
void insertNewQueen(GameBoard newBoard,int row, int column,String search) {
newBoard.board[row][column] = 1;
newBoard.lastQueenRow = row;
/**
* attack whole column except initial position
*/
for(int cursorRow = 0; cursorRow < GameBoard.WIDTH; cursorRow++) {
if(cursorRow != row)
newBoard.board[cursorRow][column] = ATTACKED;
}
/**
* attack whole row except initial position
*/
for(int cursorColumn = 0; cursorColumn < GameBoard.WIDTH; cursorColumn++) {
if(cursorColumn != column)
newBoard.board[row][cursorColumn] = ATTACKED;
}
/**
* attack diagonal left from initial position + 1 to bottom
*/
int cursorRow = row;
int cursorColumn = column;
while(cursorRow <= 7 && cursorColumn <= 7) {
if(cursorRow != row && cursorColumn != column)
newBoard.board[cursorRow][cursorColumn] = ATTACKED;
cursorRow++;
cursorColumn++;
}
/**
* attack diagonal left initial position to top
*/
cursorRow = row;
cursorColumn = column;
while(cursorRow >= 0 && cursorColumn >= 0) {
if(cursorRow != row && cursorColumn != column)
newBoard.board[cursorRow][cursorColumn] = ATTACKED;
cursorRow--;
cursorColumn--;
}
/**
* attack diagonal right initial position to bottom
*/
cursorRow = row;
cursorColumn = column;
while(cursorRow <= 7 && cursorColumn >= 0) {
if(cursorRow != row)
newBoard.board[cursorRow][cursorColumn] = ATTACKED;
cursorRow++;
cursorColumn--;
}
/**
* attack diagonal right initial position to top
*/
cursorRow = row;
cursorColumn = column;
while(cursorRow >= 0 && cursorColumn >= 7) {
if(cursorRow != row)
newBoard.board[cursorRow][cursorColumn] = ATTACKED;
cursorRow--;
cursorColumn++;
}
if(search.equals("BREADTH"))
queue.addFirst(newBoard);
else if(search.equals("DEPTH"))
queue.addFirst(newBoard);
else if(search.equals("ASTAR")) {
/**
* adicionar a uma Heap;
*/
}
}