Historias
Slashboxes
Comentarios
 
Este hilo ha sido archivado. No pueden publicarse nuevos comentarios.
Mostrar opciones Umbral:
Y recuerda: Los comentarios que siguen pertenecen a las personas que los han enviado. No somos responsables de los mismos.
  • Programa para resolver sudokus

    (Puntos:3, Interesante)
    por add (13812) el Jueves, 15 Septiembre de 2005, 14:51h (#595296)
    ( http://savannah.gnu.org/users/antonio )
    Como yo tambien odio los sudokus, he escrito este programa que los resuelve instantaneamente y que tiene el efecto de desmoralizar a los incondicionales. "Mira, la maquina lo resuelve en 0,012 ms. ¿Cuanto tardas tu?" :D
    (Por cierto, tardé menos en escribir el programa que en resolver un solo sudoku).

    #include <cstdio>

    int data[9][9];
    bool fixed[9][9];

    inline bool test( const int row, const int col, const int n ) throw()
      {
      for( int i = 0; i < row; ++i ) if( data[i][col] == n ) return false;
      for( int i = 0; i < col; ++i ) if( data[row][i] == n ) return false;
      for( int i = row + 1; i < 9; ++i )
        if( fixed[i][col] && data[i][col] == n ) return false;
      for( int i = col + 1; i < 9; ++i )
        if( fixed[row][i] && data[row][i] == n ) return false;
      const int r1 = row - ( row % 3 ), r2 = r1 + 3;
      const int c1 = col - ( col % 3 ), c2 = c1 + 3;
      for( int r = r1; r < r2; ++r )
        for( int c = c1; c < c2; ++c )
          if( data[r][c] == n && ( r != row || c != col ) &&
              ( fixed[r][c] || r < row || ( r == row && c < col ) ) ) return false;
      return true;
      }

    bool explore( int row, int col ) throw()
      {
      if( col >= 9 ) { col = 0; if( ++row >= 9 ) return true; }
      if( fixed[row][col] )
        return ( test( row, col, data[row][col] ) && explore( row, col + 1 ) );
      for( int n = 1; n < 10; ++n )
        if( test( row, col, n ) )
          { data[row][col] = n; if( explore( row, col + 1 ) ) return true; }
      return false;
      }

    int main() throw()
      {

      for( int row = 0; row < 9; ++row )
        for( int col = 0; col < 9; ++col )
          {
          if( std::scanf( "%d", &data[row][col] ) != 1 )
            { printf( "input error\n" ); return 1; }
          fixed[row][col] = ( data[row][col] != 0 );
          }

      if( explore( 0, 0 ) )
        for( int row = 0; row < 9; ++row )
          {
          for( int col = 0; col < 9; ++col )
            std::printf( " %d", data[row][col] );
          printf( "\n" );
          }
      else printf( "no hay solución\n" );
      return 0;
      }

    El formato de entrada/salida es una matriz de 9x9 cifras separadas por "whitespace", así:

    0 0 3 7 1 0 0 0 0
    0 2 0 0 0 9 4 0 0
    5 9 0 0 6 0 0 0 0
    0 0 0 8 0 0 0 6 1
    8 6 0 0 7 0 5 9 0
    7 0 0 0 9 0 8 0 0
    0 0 0 5 0 4 1 0 0
    9 5 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 3
    --
    A truly clever developer will create code so easy to understand that a less than average developer could debug it.
    Puntos de inicio:    3  puntos
    Modificador extra 'Interesante'   0  

    Total marcador:   3  
  • por isi (14123) el Jueves, 15 Septiembre de 2005, 18:37h (#595555)
    ( http://barrapunto.com/ )
    Pos eso...
    --

    Un poquito de porfavó...

    [ Padre ]