Pagini recente » Cod sursa (job #1625524) | Cod sursa (job #1676754) | Cod sursa (job #502557) | Cod sursa (job #2542642) | Cod sursa (job #2456998)
#include <iostream>
#include <fstream>
const int MAXN = 14;
const int dx[] = {-1,-1,-1,0,1,1,1,0};
const int dy[] = {-1,0,1,1,1,0,-1,-1};
using namespace std;
ifstream in("damesah.in");
ofstream out("damesah.out");
int n,cnt;
bool matrice[MAXN][MAXN];
bool validare(int x,int y){
if(matrice[x][y])
return false;
for(int i = 0 ; i < 8; i++){
int xnou = x + dx[i];
int ynou = y + dy[i];
while(1 <= xnou && xnou <= n && 1 <= ynou && ynou <= n){
if(matrice[xnou][ynou])
return false;
xnou += dx[i];
ynou += dy[i];
}
}
return true;
}
void afis(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++)
cout<<matrice[i][j]<<" ";
cout<<endl;
}
cout<<endl<<"========="<<endl;
}
void backtracking(int k){
if(k == n + 1){
if(cnt == 0){
for(int i = 1; i <= n; i++){
int j;
for(j = 1; j <= n && !matrice[i][j]; j++);
out<<j<<" ";
}
out<<"\n";
}
cnt++;
}
for(int j = 1; j <= n; j++){
if(validare(k,j)){
matrice[k][j] = true;
backtracking(k + 1);
matrice[k][j] = false;
}
}
}
int main()
{
in>>n;
backtracking(1);
out<<cnt;
return 0;
}