placequeens.cpp
来自「Data Abstraction & Problem Solving with 」· C++ 代码 · 共 41 行
CPP
41 行
#include "Queens.h"bool Board::placeQueens(Queen *queenPtr){ // Base case. Trying to place Queen in // a non-existent column. if (queenPtr->getCol() >= BOARD_SIZE) { delete queenPtr; return true; } bool isQueenPlaced = false; while (!isQueenPlaced && queenPtr->getRow() < BOARD_SIZE) { // If the queen can be attacked, then // try moving it to the next row in the // current column. if (queenPtr->isUnderAttack()) queenPtr->nextRow(); // Else put this queen on the board and // try putting a new queen in the first row // of the next column. else { setQueen(queenPtr); Queen *newQueenPtr = new Queen(0, queenPtr->getCol() + 1); isQueenPlaced = placeQueens(newQueenPtr); // If it wasn't possible to put the new // Queen in the next column, backtrack // by deleting the new Queen and // removing the last Queen placed and // moving it down one row. if (!isQueenPlaced) { delete newQueenPtr; removeQueen(); queenPtr->nextRow(); } // end if } // end if } // end while return isQueenPlaced;} // end placeQueens
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?