Sie sind auf Seite 1von 3

#include <iostream>

#include <fstream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cstring>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>

using namespace std;

ALLEGRO_DISPLAY *display = NULL;


ALLEGRO_BITMAP *tabla = NULL;
ALLEGRO_BITMAP *regina = NULL;
ALLEGRO_COLOR white;
ALLEGRO_COLOR black;

string nume;

int latura_patrat = 60;

int n,*v,sol;
char s[3];

int valid(int k);


int solutie(int k);
void afisare();
void bk(int k);

ofstream out("sah.out");

void bk(int k)
{
for (int i=1;i<=n;i++)
{
*(v+k)=i;
if (valid(k)==1)
{
if (solutie(k)==1)
afisare();
else
bk(k+1);
}
}
}

int valid(int k)
{
int i;
for (i=1;i<=k-1;i++)
if (*(v+i)==*(v+k)||(abs(*(v+k)-*(v+i))==(k-i)))
return 0;
return 1;
}

int solutie(int k)
{
if (k==n)
return 1;
return 0;
}

void afisare()
{
int i,j;
sol++;
out<<"\n Solutia: "<<sol<<'\n';
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
if (strcmp(s,"da")==0)
{
if( (i+j)%2 == 0)
al_draw_filled_rectangle( (i-1)*latura_patrat, (j-
1)*latura_patrat, i*latura_patrat, j*latura_patrat, white);
else
al_draw_filled_rectangle( (i-1)*latura_patrat, (j-
1)*latura_patrat, i*latura_patrat, j*latura_patrat, black);
}
if (*(v+i)==j)
{
out<<"R ";
if (strcmp(s,"da")==0)
al_draw_bitmap( regina, (i-1)*latura_patrat, (j-
1)*latura_patrat, 0);
}
else
out<<"_ ";

}
out<<'\n';
}

if (strcmp(s,"da")==0)
{
nume = "Solutie";
nume += std::to_string( sol);
nume += ".bmp";

al_save_bitmap( nume.data() , al_get_backbuffer( display));

al_flip_display();
}
}

int main(int argc, char const *argv[])


{
v = new int[ 100];

cout << "Introduceti marimea tablei n*n: \n n=";


cin >> n;
cout << "Solutiile au fost afisate in fisierul 'sah.out' \n";
cout << "Daca doriti aisarea grafica a solutiilor introduceti 'da', in caz
contrar introduceti 'nu':\n";
cin.get();
cin.get(s,3);

if (strcmp(s,"da")==0)
{
al_init();

int window_size = n * latura_patrat;

display = al_create_display( window_size, window_size);

al_init_image_addon();
al_init_primitives_addon();

tabla = al_load_bitmap( "tabla.bmp");


regina = al_load_bitmap( "regina.png");

white = al_map_rgb(255,255,255);
black = al_map_rgb( 0, 0, 0);
}

bk(1);
return 0;
}

Das könnte Ihnen auch gefallen