Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Bine v-am regasit, astazi va prezint un program de rezolvare sudoku, stim deja cu totii cat de grele sunt anumite jocuri de sudoku si cat de mult timp ne ia sa le si rezolvam. Eu va propun o metoda mult mai simpla prin care sa le rezolvati, tot ce aveti nevoie este un CODEBLOCKS instalat pe laptopul/ calculatorul dumneavoastra si bineinteles programul pe care vi-l voi darui eu.

Programul este putin mai jos, voi trebuie sa il copiati si sa ii dati paste in codeblocks, dupa aceea trebuie sa completati exact casutele pe care le prezinta sudok-ul dumneavoastra, in loc de spatiile libere veti adauga 0, exact cum este in exemplul de mai jos.

#include <iostream>
using namespace std;
int st[81];
int n;
int a[9][9]={
{0,0,5, 3,0,0, 0,0,0},
{8,0,0, 0,0,0, 0,2,0},
{0,7,0, 0,1,0, 5,0,0},

{4,0,0, 0,0,5, 3,0,0},
{0,1,0, 0,7,0, 0,0,6},
{0,0,3, 2,0,0, 0,8,0},

{0,6,0, 5,0,0, 0,0,9},
{0,0,4, 0,0,0, 0,3,0},
{0,0,0, 0,0,9, 7,0,0},
};
int b[9][9];
void initializare()
{
for (int i=0; i<=8; i++)
for (int j=0; j<=8; j++)
if (a[i][j]==0) n++;
for (int i=0; i<=80; i++) st[i]=0;
//copiez matricea a in matricea b pentru a se calcula i si j corespunzator p pentru matricea a initiala
for (int i=0; i<=8; i++)
for (int j=0; j<=8; j++) b[i][j]=a[i][j];
}

void afisare_matrice()
{
for (int i=0; i<=8; i++)
{ if (i%3==0) cout<<endl;
for (int j=0; j<=8; j++)
if (j%3==2) cout<<a[i][j]<<” “;
else cout<<a[i][j]<<” “;
cout<<endl;
}
cout<<endl;
cout<<“————-“<<endl;
}

void stiva_mat(int st[], int p)
{
int k=0;
for (int i=0; i<=8; i++)
if (k<=p)
for (int j=0; j<=8; j++)
{
if (b[i][j]==0) {
a[i][j]=st[k];
k++;
}
if (k>p) break;
}
}

void ret_ij(int p, int& vi, int& vj)
{
int k
while(k=0){
for (int i=0; i<=8; i++)
if (k!=p)
for (int j=0; j<=8; j++)
{
if (b[i][j]==0) k++;
if (k==p)
{
vi=i;
vj=j;
break;
}
}
}
}

void ret_inceput_patratel(int vi, int vj, int &inp, int &jnp)
{
int ri=vi%3;
int rj=vj%3;
inp=vi-ri;
jnp=vj-rj;
}

void undo_mat(int p)
{
int i, j;
ret_ij(p+1,i,j);
//a[i][j]=0;
for (int i=0; i<=8; i++)
for (int j=0; j<=8; j++) a[i][j]=b[i][j];
}

bool valid (int p)
{
int ip,jp,inp,jnp;
ret_ij(p+1,ip,jp);
for (int j=0; j<=8; j++) //verific unicitatea pe linie
if (a[ip][jp]==a[ip][j] && jp!=j) return false;
for (int i=0; i<=8; i++) //verific unicitatea pe coloana
if (a[ip][jp]==a[i][jp] && ip!=i) return false;
ret_inceput_patratel(ip,jp,inp,jnp);
for (int i=inp; i<=inp+2; i++)
for (int j=jnp; j<=jnp+2; j++)
if (a[ip][jp]==a[i][j] && ip!=i && jp!=j) return false;
return true;
}

void tipar()
{
//cout<<“test”;
for (int i=0; i<=n-1; i++) cout<<st[i]<<” “;
cout<<endl;
}

void back(int p)
{
for (int val=1; val<=9; val++)
{
st[p]=val;
stiva_mat(st,p);
if (valid(p)==false) undo_mat(p);
else
if (p==n-1) {afisare_matrice(); break;}
else back(p+1);
}
}

int main()
{
n=0;
initializare();
back(0);
return 0;
}
}

Dupa ce a-ti terminat de copiat dati un build and run si vi se va afisa solutia problemei. Bafta la rezolvari!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>