87求救qwq用vector做的

P1219 [USACO1.5] 八皇后 Checker Challenge

auto_water @ 2024-10-09 15:07:01

#include <iostream>
#include <vector>

using namespace std;

int n;
int ct=0;

bool judge(vector<int> solv, int num){
    for(int i=0; i<solv.size(); i++){
        int row = i+1;
        int column = solv[i];

        if(column==num){
            return false;
        }
        if(row-column == solv.size()+1-num){
            return false;
        }
        if(row+column == solv.size()+1+num){
            return false;
        }
    }
    return true;
}

void DFS(vector<int> solv){
    if(solv.size()==n){
        if(ct<3){
            for(int i=0; i<n; i++){
                if(i!=0)
                    cout<<" ";
                cout<<solv[i];
            }
            cout<<endl;
        }
        ct++;
        return;
    }
    for(int i=1; i<=n; i++){
        if(judge(solv, i)){
            solv.push_back(i);
            DFS(solv);
            solv.pop_back();
        }
    }
}

int main()
{
    cin>>n;
    for(int i=1; i<=n; i++){
        vector<int> solv;
        solv.push_back(i);
        DFS(solv);
    }
    cout<<ct;
    return 0;
}

|