我刚刚学会了回溯和递归,并且有一个在八皇后问题中使用它的任务。我应该提示用户输入一行,然后程序place_next_queen(bool& done)应该尝试将下一个女王放在下一列。这个程序是递归函数,我相信这就是问题所在。请看一下,任何帮助将不胜感激!如果您需要更多信息,请告诉我...... 问题是程序在第一列中正确打印出1,但是其余的都是零。
这是我的班级EightQueensProblem:
class EightQueensProblem
{
public:
//Default constructor, initializes NUM_QUEENS to 8 in initializer list
EightQueensProblem() : NUM_QUEENS(8){};
void clear_board();
void display_board(ostream& os);
bool queen_under_attack(int row, int column);
bool start_game(int row);
private:
static const int CHESSBOARD_SIZE = 8;
//2D array of int values not on the heap of dimension CHESSBOARD_SIZE x CHESSBOARD_SIZE
int chessboard[CHESSBOARD_SIZE][CHESSBOARD_SIZE];
const int NUM_QUEENS;
//int representing number of queens currently placed on the chessboard
int num_queens_on_board;
void place_queen_on_square(int row, int column);
void remove_queen_from_square(int row, int column);
void place_next_queen(bool& done);
};
void EightQueensProblem::clear_board()
{
//resets all squares on chessboard to value 0
for(int row = 0; row < CHESSBOARD_SIZE; row ++)
{
for(int column = 0; column < CHESSBOARD_SIZE; column ++)
{
chessboard[row][column] = 0;
}
}
//sets number of queens on board to 0
num_queens_on_board = 0;
}
void EightQueensProblem::display_board(ostream& os)
{
//outputs chessboard to given output stream without changing calling object
for(int row = 0; row < CHESSBOARD_SIZE; row ++)
{
for(int column = 0; column < CHESSBOARD_SIZE; column++)
{
os << chessboard[row][column] << " ";
}
os << endl;
}
}
bool EightQueensProblem::queen_under_attack(int row, int column)
{
//detect if a queen occupies a square along the southwest diagonal of square (row, col)
for(int i = 0; (row + i) < CHESSBOARD_SIZE && (column - i) >= 0; i ++)
{
if(chessboard[row + 1][column - i] != 0)
return true;
}
//along southeast diagonal
for(int i = 0; (row + i) < CHESSBOARD_SIZE && (column + i) < CHESSBOARD_SIZE; i ++)
{
if(chessboard[row + i][column + i] != 0)
return true;
}
//along northwest diagonal
for(int i = 0; (row - i) >= 0 && (column - i) >= 0; i ++)
{
if(chessboard[row - i][column - i] != 0)
return true;
}
//along northeast diagonal
for(int i = 0; (row - i) >= 0 && (column + i) < CHESSBOARD_SIZE; i ++)
{
if(chessboard[row - i][column + i] != 0)
return true;
}
//along same row
for(int i = 0; i < CHESSBOARD_SIZE; i ++)
{
if(chessboard[row][i] != 0)
return true;
}
//along same column
for(int i = 0; i < CHESSBOARD_SIZE; i ++)
{
if(chessboard[i][column] != 0)
return true;
}
return false;
}
bool EightQueensProblem::start_game(int row)
{
//places the first queen at the given row in the left most column
chessboard[row][0] = 1;
num_queens_on_board = 1;
bool done;
place_next_queen(done);
if(done == true)
return true;
else
return false;
}
void EightQueensProblem::place_queen_on_square(int row, int column)
{
//places a queen on the board at the given location (row, column)
chessboard[row][column] = column + 1;
num_queens_on_board++;
}
void EightQueensProblem::remove_queen_from_square(int row, int column)
{
//removes a queen from the board at the given location (row, column)
chessboard[row][column] = 0;
num_queens_on_board--;
}
void EightQueensProblem::place_next_queen(bool& done)
{
if(num_queens_on_board == NUM_QUEENS)
{
done = true;
}
else
{
done = false;
for(int row = 0; row < (num_queens_on_board - 1); row ++)
{
if(!queen_under_attack(row, num_queens_on_board))
{
place_queen_on_square(row, num_queens_on_board);
place_next_queen(done);
}
else
{
remove_queen_from_square(row, num_queens_on_board);
row ++;
place_queen_on_square(row, num_queens_on_board);
place_next_queen(done);
}
}
}
}
这是我的主要功能:
int main()
{
cout << "Position the first queen on the chessboard:" << endl << endl;
cout << "Enter the row for the first queen (from 1 to 8): ";
int input_row;
cin >> input_row;
cout << "From the position (" << input_row << ", 1), the EightQueensProblem has solution: " << endl << endl;
EightQueensProblem game;
game.clear_board();
game.start_game(input_row);
game.display_board(cout);
string TargetFileName = "Solutions.txt";
ofstream out(TargetFileName);
game.display_board(out);
out.close();
return 0;
}
答案 0 :(得分:0)
这不是一个解决方案,但评论时间太长了。我会在解决问题后对其进行编辑。
这根本无法解决。我直接复制了您的代码,保存了主要功能,即:
int main() {
EightQueensProblem e;
e.clear_board();
e.start_game(1);
e.display_board(cout);
}
num_queens_on_board
,永不改变,仍为0 queen_under_attack
返回true(总是)稍微具有讽刺意味的是,由于递归遵循相同的步骤,因此它永远不会停止,并且我得到了StackOverflow。我认为你需要逐步完成你的逻辑(简单地用调试器做)并修复你出错的地方。