Pagini recente » Cod sursa (job #275105) | Cod sursa (job #1339754) | Cod sursa (job #2401620) | Cod sursa (job #2082576) | Cod sursa (job #2456999)
#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 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.tie(NULL);
out.tie(NULL);
ios::sync_with_stdio(false);
in>>n;
backtracking(1);
out<<cnt;
return 0;
}