Pagini recente » Cod sursa (job #897935) | Cod sursa (job #2765024) | Cod sursa (job #3206959) | Cod sursa (job #1203386) | Cod sursa (job #1453871)
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
int n, i, contor=0;
vector <int> v;
bool as = false, as2= false;
bool checksol(){
int j, k, line, col;
for(i=0; i<n; i++){
line = i+1;
col = v[i];
for(j=1; j<n-1;j++){
if((col +j <= n) && (line + j <=n)){
if(col+j == v[line+j-1])
return false;
}
if((col-j >=1) && (line-j >=1)){
if(col-j == v[line-j-1])
return false;
}
if((col +j <= n) && (line - j >=1)){
if(col+j == v[line-j-1])
return false;
}
if((col -j >= 1) && (line + j <=n)){
if(col-j == v[line+j-1])
return false;
}
}
}
return true;
}
int main(){
freopen("damesah.in","r",stdin);
freopen("damesah.out","w",stdout);
scanf("%d" , &n);
for(i=1; i<=n; i++)
v.push_back(i);
do{
as = checksol();
if(as == true){
if(as2 == false){
as2 = true;
for(i=0; i<n; i++)
printf("%d ", v[i]);
}
contor ++;
}
}while(next_permutation(v.begin(), v.end()));
printf("\n%d", contor);
return 0;
}