Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Jorge1452 en 4 Diciembre 2014, 15:13 pm



Título: Sudoku
Publicado por: Jorge1452 en 4 Diciembre 2014, 15:13 pm
Buen día,


he estado durante estos ultimos dos días ocupado con este codigo. El objetivo no es más que controlar las columnas y las filas de un sudoku. Es decir tengo que realizar un programa que controle que en las columnas y en las filas no hallan numeros repetidos.

Este es mi codigo. Mi problema esta en que no logro que el programa detecte si hay un número repetido. Alguna indicación sería de gran ayuda

# include <iostream>



using namespace std;



typedef int Sudoku [9][9];

void sudoku(Sudoku s,int rowIndex, int colIndex);
void printSudoku(Sudoku s);
bool SudokuRowIsValid(Sudoku s, int rowIndex);
bool SudokuColumnIsValid(Sudoku s, int colIndex);
bool SudokuBlockIsValid(Sudoku s, int blockIndX, int blockIndY);



void sudoku(Sudoku s,int rowIndex, int colIndex)
{

for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++){

                    if (s[j] == 0){

                        s[j] = false;
                                    }

                    else{
                        s[j]= true;
                        s[j]= s[rowIndex][colIndex];
                    }
                }
            }



bool SudokuRowIsValid(Sudoku s, int rowIndex){

    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++){
            if (s[j]==s[rowIndex][j]){

               if (rowIndex<= 9 && rowIndex>0){

                        return true;}

                    }
                    else {

                        cout <<"sudoku ist invalid"<<endl;
                       return false;

                    }


                }

}



bool SudokuColumnIsValid(Sudoku s, int colIndex){



    for (int i = 0; i < 9; i++)
           for (int j = 0; j < 9; j++){
               if (s[j]==s[colIndex]){

                  if (colIndex<= 9 && colIndex>0){

                           return true;}

                       }
                       else {

                           cout <<"sudoku ist invalid"<<endl;
                          return false;

                       }


                   }

   }


bool SudokuBlockIsValid(Sudoku s, int blockIndX, int blockIndY){



    int vsquare = blockIndX/3;
    int hsquare = blockIndY/3;

        for (int i = vsquare * 3; i < (vsquare*3 + 3); i++){
        for (int j = hsquare * 3; j < (hsquare*3 + 3); j++){
            if (!(i == blockIndX && j == blockIndY)){

            }
                if (s[ blockIndX ][ blockIndY ]== s[j]){
                return false;
                }
            }
        }
        return true;
    }


  void printSudoku(Sudoku s)



{

        for (int i = 1; i < 9; i++)
        {

            for (int j = 1; j < 9; j++)
            {
                cout << s[j]<< endl;;
            }


        }
}




int main()


{


    Sudoku s ={{1, 2, 3, 4, 5, 6, 7, 8, 9},
               {4, 5, 6, 7, 8, 9,1 , 2, 3},
               {7, 8, 9, 1, 2, 3, 4, 5, 6},
               {2, 3, 4, 5, 6, 7, 8, 9, 1},
               {5, 6, 7, 8, 9, 1, 2, 3, 4},
               {8, 9, 1, 2, 3, 4, 5, 6, 7},
               {3, 4, 5, 6, 7, 8, 9, 1, 2},
               {6, 7, 8, 9, 1, 2, 3, 4, 5},
               {9, 1, 2, 3, 4, 5, 6, 7, 8}};

    printSudoku(s);

cout <<"valid:\n"<<SudokuColumnIsValid<<endl;

cout << "valid:\n"<<SudokuBlockIsValid<< endl;

cout << "valid:\n"<<SudokuRowIsValid<<endl;

return 0;

}


Título: Re: Sudoku
Publicado por: _Enko en 4 Diciembre 2014, 15:19 pm
Hola, en el sudoku que tienes de ejemplo, realmente no hay numeros repetidos.
Código:
{1, 2, 3, 4, 5, 6, 7, 8, 9},
{4, 5, 6, 7, 8, 9,1 , 2, 3},
{7, 8, 9, 1, 2, 3, 4, 5, 6},
{2, 3, 4, 5, 6, 7, 8, 9, 1},
{5, 6, 7, 8, 9, 1, 2, 3, 4},
{8, 9, 1, 2, 3, 4, 5, 6, 7},
{3, 4, 5, 6, 7, 8, 9, 1, 2},
{6, 7, 8, 9, 1, 2, 3, 4, 5},
{9, 1, 2, 3, 4, 5, 6, 7, 8};
Es decir, ese es un sudoku valido.

Código
  1. if (s[j] == 0){
  2.  
  3.                        s[j] = false;
  4.  
Tambien error de asignacion de datos. Asignas un bool a un int.


Saludos