Jump to content
Sign in to follow this  
gaby

Rezolvare Sudoku Folosind O Aplicatie Javascript

Recommended Posts

Mai mult ca sigur ati jucat sau ati incercat sa jucati pana acum cel putin o data Sudoku. Cu toate acestea, daca va numarati printre cei care nu au facut-o, o simpla si rapida cautare pe Google va pune la dispozitie diverse aplicatii care de care mai complexe prin care puteti juca atat SinglePlayer cat si MultiPlayer acest joc.

Ei bine, pentru rezolvarea/castigarea cat mai rapida a jocului, un tip a creat o aplicatie foarte interesnta care va poate ajuta in acest sens:

function R
(
a, // the array representing the sudoko grid

// placeholder arguments
i, // index of the last empty cell
j, // index to check the candidates for the cell 'i'
m, // candidate number for the the cell 'i'
g // flag whether 'm' is a already used in the same row|col|node as 'i'
){
// phase 1: look for an empty cell
for
(
i=80;
a[i]; // keep going if the cell isn't empty
i--||+a // decrease the index and call 'a.toString()' if we went through the whole grid
);
// phase 2: check all candidate numbers for the cell 'i'
for
(
m=10;
g=a[i]=--m; // put the candidate in the cell 'i' already and set 'g' to something truthy
// at the end of phase 2, the cell 'i' is reset to 0 for "higher" branches of the recursion
g&&R(a) // recurse if 'm' isn't already used in the same row|col|node as 'i'
)
// phase 3: check if the candidate number is used already
for(j in a) // loop through the whole grid again
g*= // turn 'g' falsy if
a[j^i==j] // we are not on the cell 'i'
^m // and the cell 'j' is set to 'm'
|| // and we are in the same row|col|node as 'i'
i/9^j/9&&i%9^j%9&&i/27^j/27|i%9/3^j%9/3
}
[/code]

Dupa cum observati, codul sursa este scris in [b]JavaScript[/b], si este suficient de bine comentat pentru a fi inteles cu usurinta.

Mai multe detalii gasiti aici: :link:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×