Cod sursa(job #2615815)

Utilizator NoodlesAndi Domnulete Noodles Data 15 mai 2020 17:08:51
Problema Problema Damelor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.6 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("damesah.in");
ofstream f2("damesah.out");

int n, c = 0;
bool executed = true;


void print(int board[13][13]){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(board[i][j] == 1){
                f2 << j << " ";
            }
        }
    }
}


bool isValid(int board[13][13], int row, int col){
    for(int i = 0; i < row; i++){
        if(board[i][col] != 0){
            return false;
        }
    }

    for(int i = row, j = col; i >= 0 && j >= 0; i--, j--){
        if(board[i][j] != 0){
            return false;
        }
    }

    for(int i = row, j = col; i >= 0 && j < n; i--, j++){
        if(board[i][j] != 0){
            return false;
        }
    }
    return true;
}

bool solveQueen(int board[13][13], int row){
    if(row >= n){
        c++;
        if(executed == true){
            print(board);
            executed = false;
            return true;
        }
    }

    bool res = false;

    for(int i = 0; i < n; i++){
        if(isValid(board, row, i) == true){
            board[row][i] = 1;
            res = solveQueen(board, row + 1);
            res = true;

            board[row][i] = 0;
        }
    }
    return res;
}

bool check(){
    int board[13][13];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            board[i][j] = 0;
        }
    }
    solveQueen(board, 0);

    f2 << endl;
    f2 << c;
    return true;
}


int main()
{
    f >> n;
    check();

    return 0;
}