Scrivere un programma che controlli se una matrice `e una
soluzione ammissibile per il gioco del Sudoku,
 
#include <stdio.h>
#define N 9 /* grandezza sudoku 9*9 */
int sudoku(int [][N]);
int main()
{
    int matrice[N][N];
    int row=0;
    int column=0;
    
    /* Inserisci i valori del sudoku */
    for(row=0; row<N; row++)
        for(column=0; column<N; column++)
                scanf("%d",&matrice[row][column]);
                
    printf("%d",sudoku(matrice));
    return 0;
       
}
int sudoku(int matrice[][N])
{
    int i=0;
    int j=0;
    int temp=0;
    int a=0;
    int b=0;
    int count=0;
    int M=3; /* grandezza sottomatrici 3*3 */
    /*************************************/    
    /*controllo se i numeri sono da 1 a 9*/
    /*************************************/
    for(i=0; i<N; i++)
        for(j=0; j<N; j++)
                if (matrice[i][j]<1 || matrice[i][j]>9)
                return 0;
                //printf("i numeri sono okn");
                
    /*******************/            
    /* controllo righe */
    /*******************/
    
    i=0;
    j=0;              
    while (i<N)
    {
        while (a<N)
        {
            temp = matrice[i][a];
            //printf("-%d-",temp);
                    for(j=a; j<N-1; j++)
                    {
                    if (temp == matrice[i][j+1])
                    //{
                    //    printf("n%d-",temp);
                        return 0;             
                    //}           
                    }a++;
        
        }
         a=0;
         j=0;
         i++;    
    }       
    //printf("le righe sono okn"); 
    
    /*********************/
    /* controllo colonne */
    /*********************/
    i=0;
    j=0;              
    while (i<N)
    {
        while (a<N)
        {
            temp = matrice[a][i];
            //printf("-%d-",temp);
                    for(j=a; j<N-1; j++)
                    {
                    if (temp == matrice[j+1][i])
                    //{
                    //    printf("n%d-",temp);
                        return 0;             
                    //}           
                    }a++;
        
        }
         a=0;
         j=0;
         i++;    
    }       
    //printf("le colonne sono okn");           
    
    i=0;
    j=0;
    a=0;
    b=0;
    
    /***********************************/
    /* controllo la prima sotto matrice*/
    /***********************************/
    while (count<M)
    {
        while (b<M)
        {
        temp = matrice[a][b];
        //printf("-%d-",temp);
        
            for (i=0; i<M; i++)
                for (j=b; j<M-1; j++)
                      if (temp == matrice[i][j+1])
                      return 0;          
        b++;   
        }
        a++;
        b=0;
        count++;   
    }
    
    /*************************************/
    /* controllo la seconda sotto matrice*/
    /*************************************/
    i=0;
    j=0;
    a=0;
    b=3;
    count=0;
    
    while (count<M)
    {
        while (b<2*M)
        {
        temp = matrice[a][b];
        //printf("-%d-",temp);
        
            for (i=0; i<M; i++)
                for (j=b; j<(2*M)-1; j++)
                      if (temp == matrice[i][j+1])
                      return 0;          
        b++;   
        }
        a++;
        b=3;
        count++;   
    }
    
    /***********************************/
    /* controllo la terza sotto matrice*/
    /***********************************/
    i=0;
    j=0;
    a=0;
    b=6;
    count=0;
    
    while (count<M)
    {
        while (b<3*M)
        {
        temp = matrice[a][b];
        //printf("-%d-",temp);
        
            for (i=0; i<M; i++)
                for (j=b; j<3*M-1; j++)
                      if (temp == matrice[i][j+1])
                      return 0;          
        b++;   
        }
        a++;
        b=6;
        count++;   
    }
    
    /************************************/
    /* controllo la quarta sotto matrice*/
    /************************************/
    i=0;
    j=0;
    a=3;
    b=0;
    count=0;
    
    while (count<M)
    {
        while (b<M)
        {
        temp = matrice[a][b];
        //printf("-%d-",temp);
        
            for (i=M; i<2*M; i++)
                for (j=b; j<M-1; j++)
                      if (temp == matrice[i][j+1])
                      return 0;          
        b++;   
        }
        a++;
        b=0;
        count++;   
    }
    
    /************************************/
    /* controllo la quinta sotto matrice*/
    /************************************/
    i=0;
    j=0;
    a=3;
    b=3;
    count=0;
    
    while (count<M)
    {
        while (b<2*M)
        {
        temp = matrice[a][b];
        //printf("-%d-",temp);
        
            for (i=M; i<2*M; i++)
                for (j=b; j<2*M-1; j++)
                      if (temp == matrice[i][j+1])
                      return 0;          
        b++;   
        }
        a++;
        b=3;
        count++;   
    }
    
    /************************************/
    /* controllo la sesta  sotto matrice*/
    /************************************/
    i=0;
    j=0;
    a=3;
    b=6;
    count=0;
    
    while (count<M)
    {
        while (b<3*M)
        {
        temp = matrice[a][b];
        //printf("-%d-",temp);
        
            for (i=M; i<2*M; i++)
                for (j=b; j<3*M-1; j++)
                      if (temp == matrice[i][j+1])
                      return 0;          
        b++;   
        }
        a++;
        b=6;
        count++;   
    }
    
    /**************************************/
    /* controllo la settima  sotto matrice*/
    /**************************************/
    i=0;
    j=0;
    a=6;
    b=0;
    count=0;
    
    while (count<M)
    {
        while (b<M)
        {
        temp = matrice[a][b];
        //printf("-%d-",temp);
        
            for (i=2*M; i<3*M; i++)
                for (j=b; j<M-1; j++)
                      if (temp == matrice[i][j+1])
                      return 0;          
        b++;   
        }
        a++;
        b=0;
        count++;   
    }
    
    /************************************/
    /* controllo l'ottava  sotto matrice*/
    /************************************/
    i=0;
    j=0;
    a=6;
    b=3;
    count=0;
    
    while (count<M)
    {
        while (b<2*M)
        {
        temp = matrice[a][b];
        //printf("-%d-",temp);
        
            for (i=2*M; i<3*M; i++)
                for (j=b; j<2*M-1; j++)
                      if (temp == matrice[i][j+1])
                      return 0;          
        b++;   
        }
        a++;
        b=3;
        count++;   
    }       
     
    /***********************************/
    /* controllo la nona  sotto matrice*/
    /***********************************/
    i=0;
    j=0;
    a=6;
    b=6;
    count=0;
    
    while (count<M)
    {
        while (b<3*M)
        {
        temp = matrice[a][b];
        //printf("-%d-",temp);
        
            for (i=2*M; i<3*M; i++)
                for (j=b; j<3*M-1; j++)
                      if (temp == matrice[i][j+1])
                      return 0;          
        b++;   
        }
        a++;
        b=6;
        count++;   
    }
          
    return 1;
} 
Ricerca appunti sul web
 
  
    Ricerca personalizzata
  
venerdì 27 febbraio 2009
Sorgenti c : Sudoku
Pubblicato da
Baiox
alle
10:40
        
 
 
Etichette: Informatica, Programmazione C
Iscriviti a:
Commenti sul post (Atom)
0 commenti:
Posta un commento