所以我这样做是为了做作业,我无法弄清楚我的错误在哪里。任何帮助都非常感谢。
我的理解是这样的。
3.然后循环
检查当前行或列是否与另一位女王发生冲突。
一个。没有冲突。推动堆叠。增加填充的行变量。填充++;向上移动。
湾有冲突。向右移。山口++;
℃。不能再向右移动了。弹出堆栈并设置为row和col。减去填充的。然后过来。山口++;然后再试一次。
int main(){
bool board[8][8];
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
board[i][j] = false;}}
int row = 0, col = 0, filled = 0;
StackLi<int> rowStack;
StackLi<int> colStack;
rowStack.push(row);
colStack.push(col);
board[row][col] = true;
//cout << "push: " << "(" << row << "," << col << ")" << endl;
row++;
while(filled < 7)
{
if(!isSafe(board,row,col) )
{
filled++;
rowStack.push(row);
colStack.push(col);
board[row][col] = true;
//cout << "push: " << "(" << row << "," << col << ")" << endl;
if(filled > 8)
{
print(board);
return 0;
}
row++;
}
else{
col++;
//cout << "move: " << "(" << row << "," << col << ")" << endl;
}
if(col > 7)
{
row = rowStack.topAndPop();
col = colStack.topAndPop();
board[row][col] = false;
cout << "pop: " << "(" << row << "," << col << ")" << endl;
filled--;
}
}
return 0;
}
bool isSafe(bool board[8][8], int row, int col)
{
for(int i = 0; i < 8; i++)
{
if(board[row][i] || board[i][col]) return true;
}
for(int i = 0; (row - i)>=0 && (col-i) >= 0; i++)
{
if(board[row-i][col-i]) return true;
}
for(int i = 0; (row - i)<=8 && (col-i) >= 0; i++)
{
if(board[row+i][col+i]) return true;
}
return false;
}