Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,102 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,677 people online right now. Registration is fast and FREE... Join Now!




Sudoku Solver

 
Reply to this topicStart new topic

Sudoku Solver

storm123
7 Oct, 2007 - 11:51 AM
Post #1

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 22


My Contributions
Hi, i'm making a 4x4 sudoku and i need to randomly pick a determinate number of items from a set in order to choose some numbers to erase. Later the user have to guess these numbers.
I've already done the sudoku and I've posted the code here:
CODE

//sudoku.cpp:

#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
#include<algorithm>
#include<iostream.h>

void main(){

srand(time(0));

    int a1;
    int b1;
    int c1;
    int d1;
    int a2;
    int b2;
    int c2;
    int d2;
        int a3;
    int b3;
    int c3;
    int d3;
        int a4;
    int b4;
    int c4;
    int d4;
        int *ptr_a1;
    int ninc;
        
    srand(time(0));
d4=0;

while(d4==0)
{  
// 1st row

a1=(rand()%4)+1;

printf("a1 = %d\n",a1);

do {
    b1=(rand()%4)+1;
}while(b1==a1);
{
printf("b1 = %d\n",b1);
}

do {
    c1=(rand()%4)+1;
}while((c1==a1)||(c1==b1));
{
printf("c1 = %d\n",c1);
}

do {
    d1=(rand()%4)+1;
}while((d1==a1)||(d1==b1)||(d1==c1));
{
printf("d1 = %d\n",d1);
}

// Column A
do {
    a2=(rand()%4)+1;
}while((a2==a1)||(a2==b1));
{
printf("a2 = %d\n",a2);
}

do {
    a3=(rand()%4)+1;
}while((a3==a1)||(a3==a2));
{
printf("a3 = %d\n",a3);
}

do {
    a4=(rand()%4)+1;
}while((a4==a1)||(a4==a2)||(a4==a3));
{
printf("a4 = %d\n",a4);
}

// Column D
do {
    d2=(rand()%4)+1;
}while((d2==c1)||(d2==d1)||(d2==a2));
{
printf("d2 = %d\n",d2);
}
do {
    d3=(rand()%4)+1;
}while((d3==d1)||(d3==d2)||(d3==a3));
{
printf("d3 = %d\n",d3);
}
do {
    d4=(rand()%4)+1;
}while((d4==d1)||(d4==d2)||(d4==d3)||(d4==a4));
{
printf("d4 = %d\n",d4);
}

// 4th row

do {
    b4=(rand()%4)+1;
}while((b4==a3)||(b4==a4)||(b4==b1)||(b4==d4));
{
printf("b4 = %d\n",b4);
}

do {
    c4=(rand()%4)+1;
}while((c4==d3)||(c4==d4)||(c4==c1)||(c4==a4)||(c4==b4));
{
printf("c4 = %d\n",c4);
}

// Central square

do {
    b2=(rand()%4)+1;
}while((b2==a1)||(b2==b1)||(b2==a2)||(b2==d2)||(b2==b4));
{
printf("b2 = %d\n",b2);
}

do {
    c2=(rand()%4)+1;
}while((c2==c1)||(c2==d1)||(c2==a2)||(c2==b2)||(c2==d2)||(c2==c4));
{
printf("c2 = %d\n",c2);
}

do {
    b3=(rand()%4)+1;
}while((b3==a3)||(b3==a4)||(b3==b4)||(b3==b1)||(b3==b2)||(b3==d3));
{
printf("b3 = %d\n",b3);
}

do {
    c3=(rand()%4)+1;
}while((c3==c4)||(c3==d4)||(c3==d3)||(c3==c1)||(c3==c2)||(c3==a3)||(c3==b3));
{
printf("c3 = %d\n",c3);
}
if (b4!=0)
   {
     break;
   }
}

// Program shows all the sudoku

cout <<""<<endl;
cout <<"Sudoku"<<endl;

cout <<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl;
cout <<a2<<" "<<b2<<" "<<c2<<" "<<d2<<endl;
cout <<a3<<" "<<b3<<" "<<c3<<" "<<d3<<endl;
cout <<a4<<" "<<b4<<" "<<c4<<" "<<d4<<endl;

cout <<""<<endl;

// The program choose and erase a number of items determinated by the user

printf("Enter how many items do you want to erase, please\n");
scanf("%s",ninc);


Now I have to erase an amount of numbers determinated by the user.
For example i have this set
int puzzle[5] = { a1,b1,c1,d1,a2 };
and i want to randomly pick 2 of these items in order to erase them.
(final set will contain all 16 numbers)
How to do that? Is there an specific function do that?
Thank you
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Sudoku Solver
7 Oct, 2007 - 12:30 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
depends on what you mean by erase.

you can use memcpy to copy the memory following the positions you want to erase into the current position. that will result in those values being removed from the array, but you'd also have to set the end value(s) of the array to zero so that they don't show up. e.g. if you want to erase positions 3 and 5 from an array source that is 16 elements long:
CODE
int source[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
memcpy(source[3], source[4],12);
source[15]=0; // source = {0,1,2,4,5,6,7,8,9,10,11,12,13,14,15,0};
memcpy(source[4], source[5],11);
source[15]=0; // source = {0,1,2,4,6,7,8,9,10,11,12,13,14,15,0,0};


if you actually want to make the array smaller, you need to first allocate it at runtime using malloc, then resize it using realloc. but remember, with both of these approaches, you're not simply leaving an empty spot in the array - you're removing that position completely.

hope that helps.

-jjh

User is offlineProfile CardPM
+Quote Post

storm123
RE: Sudoku Solver
8 Oct, 2007 - 11:50 AM
Post #3

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 22


My Contributions
I mean by erase leaving an empty place in my sudoku but no completely removing the position so later user have to input the solution and program will compare the input with the value (hiden).
So my question is how to choose randomly a determinate number of values entered by the user in order to just don't printf them when the program show the sudoku again.
I've thought about to take the number of values to "erase" entered by the user (n) and printf 16-n variables choosed randomly.
I've thougt about randomly choose these 16-n variables from a set containg all 16 variablesn but anyway i'm open to any other solucion bearing in mind i'm a begginer and i don't know how to work with arrays very well so i prefer to don't use them yet.
Thank u
User is offlineProfile CardPM
+Quote Post

storm123
RE: Sudoku Solver
8 Oct, 2007 - 12:20 PM
Post #4

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 22


My Contributions
Hi,
I'm working in a 4x4 sudoku solver and i'm a beginner in c++.
Variables are:
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
First i wrote that program
CODE

//sudoku.cpp:

#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
#include<algorithm>

void main(){

srand(time(0));

    int a1;
    int b1;
    int c1;
    int d1;
    int a2;
    int b2;
    int c2;
    int d2;
        int a3;
    int b3;
    int c3;
    int d3;
        int a4;
    int b4;
    int c4;
    int d4;

    
    srand(time(0));
    
  
// 1st row

a1=rand()*5/32768;
printf("a1 = %d\n",a1);

while((b1!=0)||(b1=a1)){
b1=rand()*5/32768;
if ((b1!=0)&&(b1!=a1)){
break;
printf("b1 %d\n",b1);
}}

printf("b1 = %d\n",b1);

while((c1!=0)||(c1=a1)||(c1=b1)){
c1=rand()*5/32768;
if ((c1!=0)&&(c1!=a1)&&(c1!=b1)){
break;
}}
printf("c1 = %d\n",c1);

while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1)){
d1=rand()*5/32768;
if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1)){
break;
}}
printf("d1 = %d\n",d1);


// Column A

while((a2!=0)||(a2=a1)||(a2=b1)){
a2=rand()*5/32768;
if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){
break;
}}

printf("a2 = %d\n",a2);

while((a3!=0)||(a3=a1)||(a3=a2)){
a3=rand()*5/32768;
if ((a3!=0)&&(a3!=a1)&&(a3!=a2)){
break;
}}

printf("a3 = %d\n",a3);


while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3)){
a4=rand()*5/32768;
if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3)){
break;
}}

printf("a4 = %d\n",a4);

// Column D

while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2)){
d2=rand()*5/32768;
if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2)){
break;
}}

printf("d2 = %d\n",d2);


while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3)){
d3=rand()*5/32768;
if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3)){
break;
}}

printf("d3 = %d\n",d3);


while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4)){
d4=rand()*5/32768;
if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4)){
break;
}}

printf("d4 = %d\n",d4);

// 4th row

while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4)){
b4=rand()*5/32768;
if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){
break;
}}

printf("b4 = %d\n",b4);

while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4)){
c4=rand()*5/32768;
if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4)){
break;
}}

printf("c4 = %d\n",c4);

// Central Square

while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4)){
b2=rand()*5/32768;
if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){
break;
}}

printf("b2 = %d\n",b2);

while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4)){
c2=rand()*5/32768;
if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){
break;
}}

printf("c2 = %d\n",c2);

while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3))){
b3=rand()*5/32768;
if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){
break;
}}

printf("b3 = %d\n",b3);

while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3)){
c3=rand()*5/32768;
if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){
break;
}}
printf("c3 = %d\n",c3);

}


It works but sometimes it reach absurd situations like this:
1 4 2 3
3 4
4 1
2 !

When that happens program stops and don't show the solced variables in that case a1=1 b1=4 c1=2 d1=3 a2=3 a3=4 a4=2 d2=4 d3=1

In order to solve that i've writen another code with a loop that doesn't stop the program until the last variable to be evaluated (c3) is not 0.

I've post it here:
CODE

//sudoku.cpp:

#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
#include<algorithm>
#include<iostream.h>

void main(){

srand(time(0));

    int a1;
    int b1;
    int c1;
    int d1;
    int a2;
    int b2;
    int c2;
    int d2;
        int a3;
    int b3;
    int c3;
    int d3;
        int a4;
    int b4;
    int c4;
    int d4;
        int *ptr_a1;
    int ninc;
        
    srand(time(0));
d4=0;

while(d4==0)
{  
// 1st row

a1=(rand()%4)+1;

printf("a1 = %d\n",a1);

do {
    b1=(rand()%4)+1;
}while(b1==a1);
{
printf("b1 = %d\n",b1);
}

do {
    c1=(rand()%4)+1;
}while((c1==a1)||(c1==b1));
{
printf("c1 = %d\n",c1);
}

do {
    d1=(rand()%4)+1;
}while((d1==a1)||(d1==b1)||(d1==c1));
{
printf("d1 = %d\n",d1);
}

// Column A
do {
    a2=(rand()%4)+1;
}while((a2==a1)||(a2==b1));
{
printf("a2 = %d\n",a2);
}

do {
    a3=(rand()%4)+1;
}while((a3==a1)||(a3==a2));
{
printf("a3 = %d\n",a3);
}

do {
    a4=(rand()%4)+1;
}while((a4==a1)||(a4==a2)||(a4==a3));
{
printf("a4 = %d\n",a4);
}

// Column D
do {
    d2=(rand()%4)+1;
}while((d2==c1)||(d2==d1)||(d2==a2));
{
printf("d2 = %d\n",d2);
}
do {
    d3=(rand()%4)+1;
}while((d3==d1)||(d3==d2)||(d3==a3));
{
printf("d3 = %d\n",d3);
}
do {
    d4=(rand()%4)+1;
}while((d4==d1)||(d4==d2)||(d4==d3)||(d4==a4));
{
printf("d4 = %d\n",d4);
}

// 4th row

do {
    b4=(rand()%4)+1;
}while((b4==a3)||(b4==a4)||(b4==b1)||(b4==d4));
{
printf("b4 = %d\n",b4);
}

do {
    c4=(rand()%4)+1;
}while((c4==d3)||(c4==d4)||(c4==c1)||(c4==a4)||(c4==b4));
{
printf("c4 = %d\n",c4);
}

// Central square

do {
    b2=(rand()%4)+1;
}while((b2==a1)||(b2==b1)||(b2==a2)||(b2==d2)||(b2==b4));
{
printf("b2 = %d\n",b2);
}

do {
    c2=(rand()%4)+1;
}while((c2==c1)||(c2==d1)||(c2==a2)||(c2==b2)||(c2==d2)||(c2==c4));
{
printf("c2 = %d\n",c2);
}

do {
    b3=(rand()%4)+1;
}while((b3==a3)||(b3==a4)||(b3==b4)||(b3==b1)||(b3==b2)||(b3==d3));
{
printf("b3 = %d\n",b3);
}

do {
    c3=(rand()%4)+1;
}while((c3==c4)||(c3==d4)||(c3==d3)||(c3==c1)||(c3==c2)||(c3==a3)||(c3==b3));
{
printf("c3 = %d\n",c3);
}
if (b4!=0)
   {
     break;
   }
}

// Program shows all the sudoku

cout <<""<<endl;
cout <<"Sudoku"<<endl;

cout <<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl;
cout <<a2<<" "<<b2<<" "<<c2<<" "<<d2<<endl;
cout <<a3<<" "<<b3<<" "<<c3<<" "<<d3<<endl;
cout <<a4<<" "<<b4<<" "<<c4<<" "<<d4<<endl;

cout <<""<<endl;

// The program choose and erase a number of items determinated by the user

printf("Enter how many items do you want to erase, please\n");
scanf("%s",ninc);



First the loop seemed work but later i noticed it reach these absurd situacions too.
Anyone know why? i've no idea what's wrong
thank you

i'm sorry about my represenation of absurd siuation in my post
correct one is here:
1 4 2 3
3 x x 4
4 x x 1
2 x x !
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Sudoku Solver
8 Oct, 2007 - 12:52 PM
Post #5

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
your method of constructing the sudoku array doesn't guarantee that you'll be able to find a solution that works. you're just assigning a random value to a cell, then trying to fit the other numbers in around it. this does not guarantee that a valid puzzle can be found; it just guarantees that you won't create an invalid puzzle, since the while loop ion the assignment will never exit.

you may want to do a search for constructing latin squares. they have many of the same properties as valid sudoku puzzles.

you may also want to do a test in your assignment statements that causes the assignment to start over if an infinite loop occurs. this can be as simple as testing whether any of 1-4 can be validly assigned.

and since this thread is directly related to a previous post, you should probably just reply in the existing thread - a moderator will probably merge the two topics eventually.

hope that helps.
-jjh
User is offlineProfile CardPM
+Quote Post

storm123
RE: Sudoku Solver
11 Dec, 2007 - 11:58 AM
Post #6

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 22


My Contributions
Hi, I've tried to write the code in a most effective way using that sum of all members of a box, row or column should be sudoku's dimensions factorial sum.
However, when the code prints the sudoku's solutions it print one value for all members of array: -9.25596e+061.
How to solve that?
Thank you!
My code:

CODE

1.  // Sudoku
2.
3.
4.  #include<stdio.h>
5.  #include<stdlib.h>
6.  #include<time.h>
7.  #include<iostream>
8.  #define MAX_FILES 10
9.  #define MAX_COLUMNES 10
10.
11. using namespace std;
12.
13.
14. int suma(int);
15.
16. //factorial sum
17. int suma(int dimensions){
18.
19.   if (dimensions<2) return dimensions;
20.      return dimensions+suma(dimensions-1);
21.
22.  }
23.
24.
25. void main(){
26.
27.      double a[MAX_ROWS][MAX_COLUMNS];
28.      int dimensions=0;
29.      int i,j;
30.      int rows, columns;
31.        int q;
32.       int k;
33.    
34.  rows=0;
35.  columns=0;
36.  dimensions=0;
37.  i=0;j=0;k=0;
38.
39.  srand(time(0));
40.
41.
42.
43.  cout <<"How many cloumns and rows are there in the sudoku? (put the value 3 times, 44.  please):\n";
45.    cin >> rows >> columns >> dimensions;
46.
47.
48. q=rows/2;
49.
50.
51.
52.   while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){
53.
54.
55.
56.    //first box
57.   do{
58.       for (i=0;i<q;i++){
59.           for(j=0;j<q;j++){
60.              a[i][j]=(rand()%dimensions)+1;
61.              }
62.       }
63.
64.   }while ((a[i][j])=!suma(dimensions));
65.  
66.
67.    //second box
68.    do{
69.       for (i=(q-1);i<rows;i++){
70.           for(j=0;j<q;j++){
71.             a[i][j]=(rand()%dimensions)+1;
72.             }
73.       }
74.   }while ((a[i][j])=!suma(dimensions));
75.
76.
77.    //third box
78.   do{
79.       for (i=0;i<q;i++){
80.           for(j=(q-1);j<columns;j++){
81.             a[i][j]=(rand()%dimensions)+1;
82.             }
83.       }
84.   }while ((a[i][j])=!suma(dimensions));
85.
86.
87.    //fourth box
88.   do{
89.       for (i=(q-1);i<rows;i++){
90.           for(j=(q-1);j<columns;j++){
91.             a[i][j]=(rand()%dimensions)+1;
92.             }
93.       }
94.   }while ((a[i][j])=!suma(dimensions));
95.
96.
97.  if ((a[i][k]==suma(dimensions))&&(a[k][j]==suma(dimensions))) {
98.       break;
99.   }
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110. }
111.
112. // All sudoku is showed
113.  for (i=0;i<rows;i++){
114.       for(j=0;j<columns;j++){
115.            cout <<""<< a[i][j] <<endl;
116.        }
117.  }
118.
119.
120.}



User is offlineProfile CardPM
+Quote Post

storm123
RE: Sudoku Solver
18 Dec, 2007 - 09:51 AM
Post #7

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 22


My Contributions
I've wrote a new code. I think it should work but it doesn't give any results when I run the program.
Can you help me telling me what's wrong, please?
CODE

// Sudoku

#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#define MAX_FILES 10
#define MAX_COLUMNES 10

using namespace std;

void main(){
  int a[MAX_FILES][MAX_COLUMNES];
  int i=0;
  int j=0;
  int dimensions=4;
  int rows=4;
  int columns=4;

// Initialization
for (i=0;i<rows;i++){
           for(j=0;j<columns;j++){
              a[i][j]=0;
              }
}


time_t seconds;
srand((unsigned)time(&seconds));


while(((a[0][0]+a[0][1]+a[0][2]+a[0][3])!=10)||((a[1][0]+a[1][1]+a[1][2]+a[1][3])!=10)||((a[2][0]+a[2][1]+a[2][2]+a[2][3])!=10)||((a[3][0]+a[3][1]+a[3][2]+a[3][3])!=10)||((a[0][0]+a[1][0]+a[2][0]+a[3][0])!=10)||((a[0][1]+a[1][1]+a[2][1]+a[3][1])!=10)||((a[0][2]+a[1][2]+a[2][2]+a[3][2])!=10)||((a[0][3]+a[1][3]+a[2][3]+a[3][3])!=10)||((a[0][0]+a[0][1]+a[1][0]+a[1][1])!=10)||((a[2][0]+a[2][1]+a[3][0]+a[3][1])!=10)||((a[0][2]+a[0][3]+a[1][2]+a[1][3])!=10)||((a[2][2]+a[2][3]+a[3][2]+a[3][3])!=10)){

    for (i=0;i<rows;i++){
           for(j=0;j<columns;j++){
              a[i][j]=(rand()%dimensions)+1;
              }
}

if(((a[0][0]+a[0][1]+a[0][2]+a[0][3])==10)&&((a[1][0]+a[1][1]+a[1][2]+a[1][3])==10)&&((a[2][0]+a[2][1]+a[2][2]+a[2][3])==10)&&((a[3][0]+a[3][1]+a[3][2]+a[3][3])==10)&&((a[0][0]+a[1][0]+a[2][0]+a[3][0])==10)&&((a[0][1]+a[1][1]+a[2][1]+a[3][1])==10)&&((a[0][2]+a[1][2]+a[2][2]+a[3][2])==10)&&((a[0][3]+a[1][3]+a[2][3]+a[3][3])==10)&&((a[0][0]+a[0][1]+a[1][0]+a[1][1])==10)&&((a[2][0]+a[2][1]+a[3][0]+a[3][1])==10)&&((a[0][2]+a[0][3]+a[1][2]+a[1][3])==10)&&((a[2][2]+a[2][3]+a[3][2]+a[3][3])==10)){


cout <<"First box"<<endl;
cout <<""<< a[0][0] <<endl;
cout <<""<< a[0][1] <<endl;
cout <<""<< a[1][0] <<endl;
cout <<""<< a[1][1] <<endl;
cout <<"----------------"<<endl;
cout <<"Second box"<<endl;
cout <<""<< a[2][0] <<endl;
cout <<""<< a[2][1] <<endl;
cout <<""<< a[3][0] <<endl;
cout <<""<< a[3][1] <<endl;
cout <<"----------------"<<endl;
cout <<"Thirth box"<<endl;
cout <<""<< a[0][2] <<endl;
cout <<""<< a[0][3] <<endl;
cout <<""<< a[1][2] <<endl;
cout <<""<< a[1][3] <<endl;
cout <<"----------------"<<endl;
cout <<"Fourth box"<<endl;
cout <<""<< a[2][2] <<endl;
cout <<""<< a[2][3] <<endl;
cout <<""<< a[3][2] <<endl;
cout <<""<< a[3][3] <<endl;
cout <<"----------------"<<endl;

         }
}

User is offlineProfile CardPM
+Quote Post

GWatt
RE: Sudoku Solver
18 Dec, 2007 - 11:08 AM
Post #8

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,178



Thanked: 18 times
Dream Kudos: 450
My Contributions
Well, I get results, but nothing useful. I suggest you revisit your logic routines and come up with a different way. Or at least find a way to make it more readable. If you have a mistake in your if statements you might never find it.
User is online!Profile CardPM
+Quote Post

baavgai
RE: Sudoku Solver
18 Dec, 2007 - 11:23 AM
Post #9

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
You're getting closer. However, there are still a number of issues. Have you ever heard the phrase, "Monkeys typing Shakespeare?" It's a little like that.

In your grid, you will need four ones, four twos, four threes, and four fours to have a valid grid at all. So, in addition to looping until you have a valid sudoku, you are also looping until you have four of each number. That's quite an order.

The other problem is that your check for valid allows invalid results. While (1+2+3+4=10), so does (1+1+4+4), etc. Here's an example that's valid according to the rule of 10:

First box: 3, 3, 2, 2
Second box: 4, 1, 1, 4
Thirth box: 1, 3, 2, 4
Fourth box: 3, 2, 4, 1

I'll offer you my version of your code to get you going. You just need to figure out what makes a valid board.

CODE

// Sudoku

#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#define ROWS 4
#define COLS 4

using namespace std;

void showBoard(int a[][COLS]) {
    cout <<"First box";
    cout <<": "<< a[0][0];
    cout <<", "<< a[0][1];
    cout <<", "<< a[1][0];
    cout <<", "<< a[1][1] <<endl;
    cout <<"Second box";
    cout <<": "<< a[2][0];
    cout <<", "<< a[2][1];
    cout <<", "<< a[3][0];
    cout <<", "<< a[3][1] <<endl;
    cout <<"Thirth box";
    cout <<": "<< a[0][2];
    cout <<", "<< a[0][3];
    cout <<", "<< a[1][2];
    cout <<", "<< a[1][3] << endl;
    cout <<"Fourth box";
    cout <<": "<< a[2][2];
    cout <<", "<< a[2][3];
    cout <<", "<< a[3][2];
    cout <<", "<< a[3][3] <<endl;
}

void swap(int a[][COLS]) {
    int row1 = rand()%ROWS;
    int col1 = rand()%COLS;
    int row2 = rand()%ROWS;
    int col2 = rand()%COLS;
    int temp = a[row1][col1];
    a[row1][col1] = a[row2][col2];
    a[row2][col2] = temp;
}

bool isValid(int a[][COLS]) {
    return ((a[0][0]+a[0][1]+a[0][2]+a[0][3])==10)
        &&((a[1][0]+a[1][1]+a[1][2]+a[1][3])==10)
        &&((a[2][0]+a[2][1]+a[2][2]+a[2][3])==10)
        &&((a[3][0]+a[3][1]+a[3][2]+a[3][3])==10)
        &&((a[0][0]+a[1][0]+a[2][0]+a[3][0])==10)
        &&((a[0][1]+a[1][1]+a[2][1]+a[3][1])==10)
        &&((a[0][2]+a[1][2]+a[2][2]+a[3][2])==10)
        &&((a[0][3]+a[1][3]+a[2][3]+a[3][3])==10)
        &&((a[0][0]+a[0][1]+a[1][0]+a[1][1])==10)
        &&((a[2][0]+a[2][1]+a[3][0]+a[3][1])==10)
        &&((a[0][2]+a[0][3]+a[1][2]+a[1][3])==10)
        &&((a[2][2]+a[2][3]+a[3][2]+a[3][3])==10);
}


int main() {
    int a[ROWS][COLS];

    time_t seconds;
    srand((unsigned)time(&seconds));
        
    // Initialization
    for (int row=0; row<ROWS; row++) {
        for(int col=0; col<COLS; col++) {
            a[row][col]=row+1;
        }
    }
    showBoard(a); // this is the initialized board
    
    int count=0;
    while(!isValid(a)) {
        swap(a);
        count++;
    }

    cout <<"count = " << count <<endl;

    showBoard(a);
    return 0;
}


The reason this works is that we pre populate the array with the numbers we want. They're in the wrong places, but that's fine. We then start swapping two positions at random. In this way, when you come up with a valid test condition, it should work.

Hope this clear. Good luck.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:00PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month