Sudoku en langage C

Bavimo Messages postés 5 Date d'inscription mercredi 29 mars 2006 Statut Membre Dernière intervention 14 avril 2006 - 9 avril 2006 à 18:34
loveslife Messages postés 3 Date d'inscription jeudi 2 décembre 2010 Statut Membre Dernière intervention 21 mai 2012 - 23 févr. 2011 à 23:22
Si cela peut aider, vous trouverez un source pour résoudre les grilles de sudoku à :
http://www.i2i.fr/
Langage C. Méthode récursive. Mis à part la saisie, 25 lignes d'instructions suffisent.
A+

5 réponses

baby3378 Messages postés 193 Date d'inscription mercredi 7 septembre 2005 Statut Membre Dernière intervention 8 juillet 2010
13 avril 2006 à 21:56
interessent :-}
0
Bavimo Messages postés 5 Date d'inscription mercredi 29 mars 2006 Statut Membre Dernière intervention 14 avril 2006
14 avril 2006 à 03:26
Merci
0
F0ul06 Messages postés 1 Date d'inscription vendredi 21 octobre 2005 Statut Membre Dernière intervention 14 avril 2008
14 avril 2008 à 23:28
Quelqu'un a cette source ?
0
stim2007 Messages postés 1 Date d'inscription dimanche 7 décembre 2008 Statut Membre Dernière intervention 7 décembre 2008
7 déc. 2008 à 21:18
/////////////////////////////////////////////////
// Guessous Hicham //
// EMSI - Rabat //
//////////////////////////////////////////////

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h> // pour abs
//**********************************************
void affichage( int m[9][9] )
{
int i,j ;
clrscr();clrscr();
gotoxy(5,5);printf("\n");
for(i=0;i<9;i++)
{ if(i%3==0&&i!=0)
{int k ;
textcolor(5);
printf(" ");
for(k=0;k<69;k++)cprintf("%c",223);// lingne
textcolor(7);
printf("\n"); }
for(j=0;j<9;j++)
{ if(j%3==0&&j!=0)
{textcolor(5);cprintf(" %c",219); textcolor(7); }
if(m[i][j]==0)
{ textcolor(6);cprintf("%7d",m[i][j]);textcolor(7); }
else
if(m[i][j]<0)
{ textcolor(3);cprintf("%7d",abs(m[i][j]));textcolor(7);}
else
printf("%7d",m[i][j]);

}
printf("\n\n");
}
}
//*************************************************
int ligne (int m[9][9],int y , int a)
{
int i;
for(i=0;i<9;i++)
if((abs(m[y][i])) == a )
return 0 ; //Existe deja

return 1 ;
}
//************************************************
int colone (int m[9][9],int x , int a)
{
int i ;
for(i=0;i<9;i++)
if(abs(m[i][x]) == a )
return 0 ; //Existe deja


return 1 ;
}
//************************************************
int sousgrille( int m[9][9], int y , int x , int a )
{
// y=ligne x=colone
int l1 , l2 ; // indice debut & fin de la sous grille (ligne)
int c1 , c2 ; // indice debut & fin de la sous grille (colone)


//------------------Limites Lignes
if( y>= 0 && y<= 2 )
{ l1=0 ; l2=3; }
if( y>= 3 && y<= 5 )
{ l1=3 ; l2=6; }
if( y>= 6 && y<= 8 )
{ l1=6 ; l2=9; }
//------------------Limites Colones
if( x>= 0 && x<= 2 )
{ c1=0 ; c2=3; }
if( x>= 3 && x<= 5 )
{ c1=3 ; c2=6; }
if( x>= 6 && x<= 8 )
{ c1=6 ; c2=9; }
//-----------------

int sauv=c1;
for( ; l1 0 ) { (*x)=8; (*y)--; }
if((*x)> 0 && (*y)>= 0 ) { (*x)--; }


for((*y) ; (*y) >= 0 ;(*y)--)
{ while((*x)>=0)
{
if( ((m[*y][(*x)])<0) )
return ;

(*x)--;
}
if((*x)<0 && (*y) > 0 ) (*x)=8;
}
if((*y)<0 && *x >=0) (*y)=0;
if((*y)<0 && (*x)<0) {
textcolor(180);
gotoxy(30,25);
cprintf("Pas de Solutions !");
getch();getch();
_exit(0);
}

}

//************************************************************
void backtrackin(int m[9][9],int *y , int *x )
{

int a, inc ;

m[*y][*x]=0 ;
recule(m,y,x);

if (m[*y][*x] > -9 ) // Compris entre -1 et -8
{ inc = 1 ;
do{
a= m[*y][*x]-inc;
inc++;
if(colone(m,*x,-a)&&ligne(m,*y,-a)&&sousgrille(m,*y,*x,-a))
{
m[*y][*x]=a;
return; //----------> return a sudo
}
}while(a > -9);

if( a <= -9)
backtrackin(m,y,x);

}
else if (m[*y][*x] == (-9) )
{

backtrackin(m,y,x);


}


}
//*************************************************
void sudo(int m[9][9])
{
int y ,x ;
int a ;

for(y=0;y<9;y++)
for(x=0;x<9;x++)
if(m[y][x]== 0 )
{
for(a=1 ; a<=9 ; a++)
{
if(colone(m,x,a) && ligne(m,y,a) && sousgrille(m,y,x,a) )
{
m[y][x] = -a ;
break;
}
}

if(a>9) // on a rien pu mettre dans la case ou il ya 0
{
backtrackin(m,&y,&x);
}

}




affichage(m);
}
//************************************************
void remplissage(int m[9][9])
{
struct les_x{
int lg ; // lg : Limite gauche
int ld ; // ld : limite droite
}a[9];
struct les_y{
int lg ;
int ld ;
}b[9];
int i ;
//-----<<< definition des borne >>>
//----------------------- les x
a[0].lg=48,a[0].ld=55 ;
for(i=1;i<9;i++)
{ if(i%3==0&&i!=0)
{ a[i].lg=a[i-1].ld+81 ;
a[i].ld=a[i].lg+7 ;
}
else
{ a[i].lg=a[i-1].ld+49;
a[i].ld=a[i].lg+7;
}
}

//---------------------- les y
b[0].lg=32,b[0].ld=39 ;
for(i=1;i<9;i++)
{ if(i%3==0&&i!=0)
{ b[i].lg=b[i-1].ld+17 ;
b[i].ld=b[i].lg+7 ;
}
else
{ b[i].lg=b[i-1].ld+9;
b[i].ld=b[i].lg+7;
}
}



int x, y, clic;
clrscr();
_AX=0; geninterrupt(0x33); // initialise le driver de la souris
_AX=1; geninterrupt(0x33); // affiche le curseur de la souris
_setcursortype(_NOCURSOR); // on retire le "curseur du clavier"

affichage(m);

do
{
_AX=3; geninterrupt(0x33); // recherche des infos de la souris
clic=_BX; // le clic se trouve dans _BX
x=_CX; // l'abscisse dans le registre _CX
y=_DX; // la coordonn'e dans _DX

// clic gauche
if (clic & 1)
{

int i,j;
for(i=0;i<9;i++)
for(j=0;j<9;j++)
if( x >= a[j].lg && x <= a[j].ld && y >= b[i].lg && y <= b[i].ld)
{ int temp ;
do
{
affichage(m);
gotoxy(10,25) ;
printf("===> Valeur pour la case- M[%d][%d] : ",i,j);
scanf("%d",&temp);
if(temp)
if(colone(m,j,temp)&&ligne(m,i,temp)&&sousgrille(m,i,j,temp))
;
else
{
affichage(m);
printf("Impossible de mettre %d !!",temp);temp=-1 ;
getch(); }
}while(temp<0 || temp>9);
m[i][j]=temp ;
affichage(m);clrscr();affichage(m);
}



}



}
while (!kbhit());
}



//*************************************************
main()
{
int m[9][9],i,j;


for(i=0;i<9;i++)
for(j=0;j<9;j++)
m[i][j]=0; // Mise a Zero de la matrice


remplissage(m);

affichage(m);

sudo(m);

gotoxy(20,25);
textcolor(89);
cprintf("\a=**= FIN !! ESC pour Terminer =**=");

char c ;
do {
fflush(stdin);
c=getch();

}while(c!=27);

}</math.h></dos.h></stdlib.h></conio.h></stdio.h>
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
loveslife Messages postés 3 Date d'inscription jeudi 2 décembre 2010 Statut Membre Dernière intervention 21 mai 2012
23 févr. 2011 à 23:22
bsr stim2007,
STP je voudrais b1 de me donner le lien exacte de ce programme de Guessous Hicham.
j'attend ta rép.
mrc d'avance.
0
Rejoignez-nous