Cod sursa(job #1453865)

Utilizator Player1Player 1 Player1 Data 24 iunie 2015 21:22:29
Problema Problema Damelor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>

using namespace std;

int n, i;
vector <int> v;	
bool as = false;

bool checksol(){
	int j, k, line, col;
	int a[15][15] = {{0}};

	for(i=0; i<n; i++)
		a[i+1][v[i]] = 1;

	for(i=0; i<n; i++){
		line = i+1;
		col = v[i];
		for(j=1; j<n;j++){
			if((col +j <= n) && (line + j <=n)){
				if(a[line+j][col+j] == 1)
					return false;
			}
			if((col-j >=1) && (line-j >=1)){
				if(a[line-j][col-j] == 1)
					return false;
			}
			if((col +j <= n) && (line - j >=1)){
				if(a[line-j][col+j] == 1)
					return false;
			}
			if((col -j >= 1) && (line + j <=n)){
				if(a[line+j][col-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)
			for(i=0; i<n; i++)
				printf("%d ", v[i]);				
	}while((next_permutation(v.begin(), v.end())) && (as == false));

	return 0;
}