elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: AIO elhacker.NET 2021 Compilación herramientas análisis y desinfección malware


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Sudoku
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Sudoku  (Leído 1,627 veces)
Jorge1452

Desconectado Desconectado

Mensajes: 1


Ver Perfil
Sudoku
« 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;

}


En línea

_Enko


Desconectado Desconectado

Mensajes: 538



Ver Perfil WWW
Re: Sudoku
« Respuesta #1 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


« Última modificación: 4 Diciembre 2014, 15:30 pm por _Enko » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Sudoku.. « 1 2 »
Programación Visual Basic
loco! 16 7,742 Último mensaje 3 Junio 2006, 13:40 pm
por karmany
Sudoku « 1 2 »
Programación C/C++
Jaua10 14 5,665 Último mensaje 24 Noviembre 2014, 05:23 am
por Jaua10
sudoku
Programación C/C++
cesar2015 3 1,986 Último mensaje 8 Noviembre 2015, 21:04 pm
por ivancea96
sudoku en dev c++
Programación C/C++
andres timo 2 3,333 Último mensaje 3 Marzo 2019, 02:50 am
por andres timo
javascript - Generador Sudoku
Desarrollo Web
TickTack 1 2,205 Último mensaje 4 Enero 2020, 16:22 pm
por @XSStringManolo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines