karlangadas

sudoku

Sep 16th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define M 9
  4. /*
  5.  *
  6.  */
  7.  
  8.  
  9. int get_next_position(int sudoku[M][M], int* f, int* c){
  10.     int i,j;
  11.     for ( i=0;i<M;i++){
  12.         for (j=0;j<M;j++){
  13.             if (sudoku[i][j]==0){
  14.                 *f=i;*c=j;
  15.                 return 1;
  16.             }
  17.         }
  18.     }
  19.      return 0;
  20. }
  21. void pprint(int sudoku[M][M]){
  22.     int i,j;
  23.     for (i=0;i<M;i++){
  24.         for (j=0;j<M;j++){
  25.             printf("%d ",sudoku[i][j]);
  26.         }
  27.         printf("\n");
  28.     }
  29. }
  30.  
  31. int numvalido(int sudoku[M][M],int f,int c,int num){
  32.     //filas y columnas
  33.     int i,j;
  34.     for (i=0;i<M;i++){
  35.         if(sudoku[i][c]==num)
  36.             return 0;
  37.         if(sudoku[f][i]==num)
  38.             return 0;
  39.     }
  40.    
  41.     int i_cuad = (f/3)*3; // OJO: Es división entera porque los operandos son enteros
  42.     int j_cuad = (c/3)*3; // OJO: Es división entera porque los operandos son enteros
  43.     for (i = 0; i < 3; i++)
  44.         for (j = 0; j < 3; j++)
  45.             if (sudoku[i+i_cuad][j+j_cuad] == num)
  46.                 return 0;
  47.    
  48.    
  49.    
  50.     //cuadrados
  51.     /*int f_cuad = (f+3)/3;
  52.     int c_cuad = (c+3)/3;
  53.     for (i = 0; i < 3; i++)
  54.         for (j = 0; j < 3; j++)
  55.             if (sudoku[i+(f_cuad-1)*3][j+(c_cuad-1)*3] == num)
  56.                 return 0;*/
  57.    
  58.         return 1;
  59. }
  60.  
  61.  
  62. int solve_sudoku(int sudoku[M][M]){
  63.     int f, c,num;
  64.     /* Caso Base: No hay posiciones disponibles*/
  65.     if (!get_next_position(sudoku, &f, &c))
  66.        return 1;
  67.    
  68.     for(num=1;num<M+1;num++){
  69.         if(numvalido(sudoku,f,c,num)){
  70.             sudoku[f][c]=num;
  71.             if(solve_sudoku(sudoku))
  72.                 return 1;
  73.              sudoku[f][c]=0;
  74.            
  75.         }
  76.     }
  77.     return 0;
  78.    
  79. }
  80. int main() {
  81.    
  82.     int sudoku[M][M] = {{3, 0, 6, 5, 0, 8, 4, 0, 0},
  83.                       {5, 2, 0, 0, 0, 0, 0, 0, 0},
  84.                       {0, 8, 7, 0, 0, 0, 0, 3, 1},
  85.                       {0, 0, 3, 0, 1, 0, 0, 8, 0},
  86.                       {9, 0, 0, 8, 6, 3, 0, 0, 5},
  87.                       {0, 5, 0, 0, 9, 0, 6, 0, 0},
  88.                       {1, 3, 0, 0, 0, 0, 2, 5, 0},
  89.                       {0, 0, 0, 0, 0, 0, 0, 7, 4},
  90.                       {0, 0, 5, 2, 0, 6, 3, 0, 0}};
  91.     if (solve_sudoku(sudoku))
  92.           pprint(sudoku);
  93.     else
  94.          printf("No existe solución");
  95.    
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment