想知道最后输出的时候j为什么是n+1到2*n+1

P1219 [USACO1.5] 八皇后 Checker Challenge

oneday001 @ 2023-11-23 14:32:41


#include<vector>
using namespace std;
//只需要检查左上角,上面,右上角
int n;
int cnt;
bool check(vector<vector<int>>& board, int i, int j) {
    //左上
    for (int curi = i - 1, curj = j - 1; curi >= 1 && curj >= 1; curi--, curj--) {
        if (board[curi][curj])return false;
    }
    //上
    for (int cur = i - 1; cur >= 1; cur--) {
        if (board[cur][j])return false;
    }
    //右上
    for (int curi = i - 1, curj = j + 1; curi >= 1 && curj <= n; curi--, curj++) {
        if (board[curi][curj])return false;
    }
    return true;
}
void Solve_N(vector<vector<int>>& board, vector<vector<int>>& result, vector<int>& path, int index) {
    if (index == n + 1) {
        result.push_back(path);
        return;
    }
    for (int j = 1; j <= n; j++) {
        if (check(board, index, j)) {
            path.push_back(j);
            board[index][j] = 1;
            Solve_N(board, result, path, index + 1);
            path.pop_back();
            board[index][j] = 0;
        }
    }
    return;
}
int main() {
    cin >> n;
    vector<vector<int>>board(n + 1, vector<int>(n + 1, 0));
    vector<vector<int>>result;
    vector<int>path(n + 1, 0);
    Solve_N(board, result, path, 1);
    for (int i = 0; i < 3; i++) {
        for (int j = n + 1; j < 2 * n + 1; j++) {//这里为什么j不是从0开始到n
            cout << result[i][j] << " ";
        }
        cout << endl;
    }
    cout << result.size();
    return 0;
}```

|