这段n皇后的代码有什么问题吗
#ifndef N_QUEENS_QUESTION
#define N_QUEENS_QUESTION
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void n_queens(int n) {
vector<vector<bool>> attack; //攻击位置数组,0-安全,1-会受攻击
vector<string> queen; //皇后位置数组
vector<vector<string>> solve; //解决方法数组
//初始化
for (int i = 0; i < n; i++) {
vector<bool> a_line;
for (int j = 0; j < n; j++) {
a_line.push_back(false);
}
attack.push_back(a_line);
string q_line;
q_line.assign(n, '.');
queen.push_back(q_line);
}
void back_track(int k, int n, vector<vector<bool>> &attack, vector<string> &queen, vector<vector<string>> &solve);
back_track(1, n, attack, queen, solve);
//展示结果
cout << n << "皇后有" << solve.size() << "个解法,如下:" << endl;
cout << "..........................................................." << endl;
for (vector < vector<string>>::iterator it = solve.begin(); it != solve.end();it++) {
for (vector<string>::iterator it2 = it->begin(); it2 != it->end(); it++) {
cout << *it2 << endl;
}
cout << "..........................................................." << endl;
}
}
void put_queen(int x,int y, vector<vector<bool>> &attack) {
int direct_x[] = { -1,-1,-1,0,0,1,1,1 }; //x方向数组,长度为8
int direct_y[] = { -1,0,1,-1,1,-1,0,1 }; //y方向数组,长度为8
attack[x][y] = 1;
for (int i = 1; i < attack.size(); i++) { //每个方向最多前进i次
for (int j = 0; j < 8; j++) {
if(x + i * direct_x[j]>0&& x + i * direct_x[j]<attack.size()&& y + i * direct_y[j]>0 && y + i * direct_y[j] < attack.size())
attack[x + i * direct_x[j]][y + i * direct_y[j]] = 1;
}
}
}
void back_track(int k,int n, vector<vector<bool>> &attack, vector<string> &queen, vector<vector<string>> &solve){ //k-当前行数
if (k == n+1) {
solve.push_back(queen);
return; //递归边界
}
for (int i = 0; i < n; i++) {
vector<vector<bool>> tmp = attack;
if (!attack[k][i]) {
queen[k][i] = 'Q';
put_queen(k, i, attack);
back_track(k + 1, n,attack, queen, solve); //归纳项
attack = tmp;
queen[k][i] = '.'; //若归纳项没能走到递归边界,则还原,试探这一层的下一个位置.
}
}
}
#endif