Pagini recente » Monitorul de evaluare | Cod sursa (job #353441) | Cod sursa (job #996746) | Cod sursa (job #1337089) | Cod sursa (job #1730898)
#include <fstream>
#include <cmath>
using namespace std ;
ifstream f ("damesah.in") ;
ofstream g ("damesah.out") ;
int n , nr_sol , memo[15] , col[15] , diag_prin[30] , diag_sec[30] ;
void afis ()
{
++nr_sol ;
if ( nr_sol == 1 )
{
for ( int j = 1 ; j <= n ; ++j )
g << memo[j] << " " ;
g << "\n" ;
}
}
void back ( int k )
{
if ( k == n + 1 )
afis () ;
else if ( k <= n )
{
for ( int j = 1 ; j <= n ; ++j )
if ( col[j] == 0 && diag_prin[j-k+n] == 0 && diag_sec[j+k] == 0 )
{
memo[k] = j ;
col[j] = diag_prin[j-k+n] = diag_sec[j+k] = 1 ;
back ( k + 1 ) ;
col[j] = diag_prin[j-k+n] = diag_sec[j+k] = 0 ;
}
}
}
int main ()
{
f >> n ;
back ( 1 ) ;
g << nr_sol ;
}