황후

7839 단어 귀속
---컨텐츠 복원 시작 --
#include "stdafx.h"
#include <iostream>
#include <fstream> // 
#include <iomanip>
#include <cstdlib>

using namespace std;
void queenSolve(int rowCurrent, int n, int *&queenlist, int &count, ofstream &os);
void Print(int n, int *&queenlist, ofstream &os);
bool Check(int rowCurrent, int *&queelist);

int main()
{
    int n;
    cout << " n:  " << endl;
    cin >> n;
    if (n<4)
    {
        cerr << " 4" << endl;
        return 0;
    }

    int *queenlist = new int[n];
    int count = 0;
    ofstream os;
    os.open("result.txt");
    queenSolve(0,n, queenlist, count, os);
    cout << " " << count << " " << endl;
    os.close();

    system("pause");
    return 0;
}
void Print(int n, int *&queenlist, ofstream &os){
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++){
            os <<(queenlist[i] == j ? 1 : 0);
            os << setw(2); // n ,<iomanip>
        }
        os << "
"; } os << "
"; } bool Check(int rowCurrent, int *&queelist){ for (int i = 0; i < rowCurrent; i++){ if (queelist[rowCurrent] == queelist[i]) return false; if (abs(rowCurrent - i) == abs(queelist[rowCurrent] - queelist[i])) //<cstdlib> return false; } return true; } void queenSolve(int rowCurrent, int n, int *&queenlist, int &count, ofstream &os) { if (rowCurrent == n) { ++count; os << " " << count << " " << endl; Print(n,queenlist,os); } else{ for (int i = 0; i < n; i++) { queenlist[rowCurrent] = i; if (Check(rowCurrent, queenlist)) queenSolve(rowCurrent+1, n, queenlist, count, os); } } }

 
--- 컨텐츠 복구 완료 --

좋은 웹페이지 즐겨찾기