Foros aprenderaprogramar.com

Aprender a programar => Aprender a programar desde cero => Mensaje iniciado por: nathaliness@gmail.com en 16 de Diciembre 2014, 06:34

Título: Sudoku C++ Ayuda con proyecto cplusplus
Publicado por: nathaliness@gmail.com en 16 de Diciembre 2014, 06:34
Hola necesito toda la ayuda posible para realizar un sudoku.

Contiene las siguientes reglas. Es muy complicado, he encontrado muchas partes de codigo, pero es imposible de compilarlo, me he dado casi por vencida, a pesar de q reviso toda la materia y la entiendo, me es dificil aplicarla en la pratica para realizar este proyecto.

Necesito ayuda porfavor!

Los requerimientos del programa

- Programa muestra la tabla de sudoku, claramente marcada, presionando la letra b, la tabla se mostrara vacia.
- Deben ser ingresados 17 digitos al azar y debe permitir añadir numeros del 1 al  9 en las casillas vacias con las teclas de direccion. Los datos ingresados pueden ser editados.
- El programa revisara si el numero ingresado es valido o no
- Menu ayuda con las reglas
- Presionando h, el usuario receibira la vista de los numeros validos para esa celda
- Permtira guardar el sudoku en un archivo y tb permitira cargar un archivo
- Contador de tiempo desdeel principio
- Deshacer y rehacer movimientos
- Añadir cuantos numeros indique el usuario al azar
- Inicar que no hay mas movimientos
Título: Re:Sudoku_Ayuda con proyecto
Publicado por: Ogramar en 17 de Diciembre 2014, 09:36
Hola, esto es complejo de resolver... No has dicho en qué lenguaje estás trabajando ¿? Pega el código que tengas (o adjúntalo como se indica en https://www.aprenderaprogramar.com/foros/index.php?topic=1460.0) para tratar de mirarlo

Saludos
Título: Re:Sudoku_Ayuda con proyecto
Publicado por: nathaliness@gmail.com en 20 de Enero 2015, 02:29
Hola esta es la parte del codigo q tengo, pero igual no compila seq tiene algo mal.

Código: [Seleccionar]
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

const int Max_C = 9;
const int Max_L = 10;


void Suk_Main_Menu (int S[9][9], int, int, int, char[]);
void start(int S[9][9], int, int, int ,char[]);
void genTab ( int S[9][9]);          // matrix  9*9
void displaytable ( int S[9][9], int, int, int &Free);         
int fill ( int S[9][9], int l, int c);     // put a number in choosen cell


int main() {

int S[Max_L][Max_C], l, c, Free;
char Name[20];
Suk_Main_Menu( S, l, c, Free,Name);
system ("PAUSE");
     return 0;
}


//INITIAL MENU
void Suk_Main_Menu(int S[9][9], int l, int c, int Free, char Name[20] )
{   
    char option; 
    cout << "\n\n\n\n\n\n\t\t\t" "S.U.D.O.K.U    G.A.M.E:" << endl << endl ;
    cout << "\t\t\t""   Option:      Press:" << endl;
    cout << "\t\t\t""  NEW GAME        b" << endl;
    cout << "\t\t\t""  LOAD GAME       l" << endl;
    cout << "\t\t\t""  HELP/MANUAL     h" << endl;
    cout << "\n\n\t" "Choose your option:" << option;
    cin >> option;
    option=getchar();
     
    switch(option)
    {
        case 'b' : start(S, l, c, Free, Name);
                    break;
             
       /* case 'l' : open( S, l, c, Free, Name);
                    break;       


case 'h': help( S, l, c, Free, Name);
                    break;

default: ("ERROR");   */   

     }   
}

//VOID START
void start(int S[9][9], int l, int c, int Free,char Name[20]){

char s;
genTab ( S );
displaytable ( S,l,c, Free);}


//VOID SUDOKU
void genTab ( int S[9][9])        // Number of free cells
{
    for (int i=0; i<9; i++)
      for (int j=0; j<9; j++)
      S [i][j]= 0;
}

//VOID DISPLAYTABLE (PRINT OUT THE SUDOKU TABLE 9x9)
void displaytable ( int S[9][9], int l, int c, int &Free)
{
  Free = 81;
  for (int i=0; i<9; i++)
  for (int j = 0; j < 9; j++)
S[i][j] = j; //0 1 2 3 ... 8 in every row
//printing it like a 9*9 table, split into 3x3 blocks, like in your example
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cout<<S[i][j]<<" ";
//every third column draw a vertical line
if ((j+1)%3 == 0)
cout<<char(219)<<" ";
}
cout<<endl;
//every third row draw a horizontal line
if ((i+1)%3 == 0)
{
for (int j = 0; j < 23; j++)
cout<<char(219);
cout<<endl;
}

for (int i = 0; i < 17; i++)
{
   int x = rand()%(9-1);
   int y = rand()%(9-1);
   S[x][y] = rand() % 9+1;
}
}

   Porfavor si alguien pudiera guiarme un poco.
Título: Re:Sudoku_Ayuda con proyecto
Publicado por: nathaliness@gmail.com en 20 de Enero 2015, 02:31
es Dev C++
Título: Re:Sudoku C++_Ayuda con proyecto
Publicado por: Ogramar en 20 de Enero 2015, 16:46
Hola, prueba con este código:

Código: [Seleccionar]
/* SU DOKU */

#include <iostream.h>
void main()
{
int k[9][9],K[9][9];
int i,j,i1,j1,i2,j2;
int error,temp;
int count=0;

for(i=0;i<9;i++)
for(j=0;j<9;j++)
K[i][j]=0;

for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
cin>>K[i][j];
k[i][j]=K[i][j];
}
cout<<"O.K.? (Enter 0 if OK, 1 to update): ";
cin>>error;
if(error==0)
goto matrixvalidation;

matrixupdation:
while(1)
{
cout<<"Enter Row, Col, Revised number:(0 to exit) ";
cin>>i;
if(i==0)break;
cin>>j>>temp;
if(i>0&&j>0&&temp>=0&&i<10&&j<10&&temp<10)
{
K[i-1][j-1]=temp;
k[i-1][j-1]=temp;
}
else
cout<<"Enter row/column 1 to 9 & number 0 to 9 only.
";
}

matrixvalidation:
cout<<"
Input matrix:
";
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
cout<<k[i][j]<<" ";
cout<<"
";
}

for(i=0;i<9;i++)
for(j=0;j<9;j++)
if(k[i][j]<0||k[i][j]>9)
{
cout<<"
"<<i+1<<" "<<j+1<<" "<<k[i][j];
cout<<"
Input matrix error.";
cout<<"
Numbers should be 1 to 9 only.

";
goto matrixupdation;
}

for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
if(k[i][j]==0)continue;
error=0;
for(i1=0;i1<9;i1++)
if(i!=i1&&k[i][j]==k[i1][j])
{
error=1;
i2=i1;
j2=j;
}
for(j1=0;j1<9;j1++)
if(j!=j1&&k[i][j]==k[i][j1])
{
error=1;
i2=i;
j2=j1;
}
for(i1=0;i1<9;i1++)
for(j1=0;j1<9;j1++)
if((i!=i1||j!=j1)&&i/3==i1/3&&j/3==j1/3&&k[i][j]==k[i1][j1])
{
error=1;
i2=i1;
j2=j1;
}
if(error)
{
cout<<"
"<<i+1<<" "<<j+1<<" "<<k[i][j];
cout<<"
"<<i2+1<<" "<<j2+1<<" "<<k[i2][j2];
cout<<"
Input matrix error.";
cout<<"
A number has been repeated in the same row, col or
block.

";
goto matrixupdation;
}
}

/* Logic starts: */
for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
if(K[i][j]>0) goto chksol;
for(k[i][j]++;k[i][j]<=9;k[i][j]++)
{
error=0;
for(i1=0;i1<9;i1++)
if(i!=i1&&k[i][j]==k[i1][j])error=1;
for(j1=0;j1<9;j1++)
if(j!=j1&&k[i][j]==k[i][j1])error=1;
for(i1=0;i1<9;i1++)
for(j1=0;j1<9;j1++)
if((i!=i1||j!=j1)&&i/3==i1/3&&j/3==j1/3&&k[i][j]==k[i1][j1])
error=1;
if(error==0)break;
}
if(k[i][j]>9)
{
k[i][j]=0;
do
{
if(i==0&&j==0)goto nomoresol;
if(j>0)j--;else{j=8;i--;}
}while(K[i][j]>0);
j--;
}
chksol: if(i==8&&j==8)
{
cout<<"
Solution: "<<++count<<"
";
for(i1=0;i1<9;i1++)
{
for(j1=0;j1<9;j1++)
cout<<k[i1][j1]<<" ";
cout<<"
";
}
if(count==50)
{
cout<<"
Too many solutions.
Not checking for more
solutions.

";
return;
}

while(K[i][j]>0)
{
if(i==0&&j==0)goto nomoresol;
if(j>0)j--;else{j=8;i--;}
}
k[i][j]=0;
do
{
if(i==0&&j==0)goto nomoresol;
if(j>0)j--;else{j=8;i--;}
}while(K[i][j]>0);
j--;
}
}
nomoresol:
if(count>0)
cout<<"
No more solutions.

";
else
cout<<"No solution.

";
}