Episodic Journal

Iwan Njoto Sandjaja

Sudoku

| Comments

I like mind games. Recently, we learn Sudoku. My wife and I like Sudoku. I am always lose to my wife. She is good at doing same thing over and over. I hate doing the same thing. Luckily, I like programming. After spent several hours before lunch, I finish my sudoku solver.

You can have text file like this one (p1.txt) :

8 0 6 0 0 7 0 0 5  
0 7 1 0 4 0 0 3 0  
0 0 0 0 6 2 0 1 9   
7 0 2 0 0 0 0 0 0   
0 6 4 0 0 0 2 8 0  
0 0 0 0 0 0 1 0 7    
6 2 0 1 9 0 0 0 0   
0 3 0 0 7 0 9 4 0   
4 0 0 5 0 0 6 0 2  

And after you compile succesfully, you can call sudokusolver.exe < p1.txt > s1.txt and see the result in s1.txt

8 9 6 3 1 7 4 2 5  
2 7 1 9 4 5 8 3 6  
3 4 5 8 6 2 7 1 9  
7 8 2 6 3 1 5 9 4  
1 6 4 7 5 9 2 8 3  
9 5 3 4 2 8 1 6 7  
6 2 7 1 9 4 3 5 8  
5 3 8 2 7 6 9 4 1  
4 1 9 5 8 3 6 7 2  

Solved!!   
(sudokusolver.c) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***********************************************************/
//
// Filename: sandjaja.c
// Author : Sandjaja, Iwan Njoto
// Last Updated: August 16, 2007
// 
// Program Description:
// Sudoko Solver
// 
//***********************************************************/

#include <stdlib.h>
#include <stdio.h>

#define N 9


int solver[N][N][N];
int answer[N][N];

void setfalse (int number, int x, int y){ 
  int i,j;
  int dx,dy;//, qx, qy ;
  
  for (i = 0; i < N; i++) {
      if (i!=y) solver[x][i][number] = 0;//vertical
      if (i!=x) solver[i][y][number] = 0;//horisontal
      if (i!=number) solver[x][y][i] = 0;//self
  }
  
  //-- 3x3 box
  dx = (x / 3) * 3; //qx = x % 3;
  dy = (y / 3) * 3; //qx = x % 3;        
  for (i = 0; i < 3; i++) {
      for (j = 0; j < 3; j++) {
          if (((i+dx)==y) || ((j+dy)==y)) continue;
          solver[i+dx][j+dy][number] = 0;           
      }
  }
}
int findnewtrue(){
  int i,j,k;
  int newtrue;
  newtrue = 0;
  for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
          if (answer[i][j]!=0) continue;
          for (k = 0; k < N; k++) {
              if(solver[i][j][k] == 1){
                  if(findonlytrue(i,j,k)){
                      newtrue = 1;
                      setfalse(k, i, j);
                      answer[i][j] = k+1;
                  }
              }
          }
      }
  }
  return newtrue;
}

int findonlytrue (int x, int y, int number){  
  int i,j;
  int dx,dy;
  int counterx,countery, counterbox;
  counterx = countery = 0;
  for (i = 0; i < N; i++) {
      if(solver[x][i][number]==1) countery++;
      if(solver[i][y][number]==1) counterx++;
  }
  
  if ((counterx == 1)||(countery == 1) ){        
      return 1;
  }
              
  //-- 3x3 box
  counterbox = 0;
  dx = (x / 3) * 3;
  dy = (y / 3) * 3;
  for (i = 0; i < 3; i++) {
      for (j = 0; j < 3; j++) {
          if (solver[i+dx][j+dy][number]==1) counterbox++;
      }
  }
  if (counterbox == 1) {        
      return 1;
  }
  return 0;
}
void initsolver() {  
  int i,j,k;
  for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
          for (k = 0; k < N; k++) {
              solver[i][j][k] = 1;
          }
      }
  }
}

void printanswer(){
  int i,j;
  for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {     
          printf("%d ",answer[i][j]);
      }
      printf("\n");
  }
  printf("\n");
}

void readproblem() {
  int number;
  int i,j;
  for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
          scanf("%d",&number);
          if (number) {
              setfalse(number-1, i, j);
              answer[i][j] = number;
          }
      }
  }
  printanswer();
}

int searchanswer(){
  int i,j,k;
  int counter, id;
  int newanswer;
  newanswer = 0;
  for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
          counter = 0;
          id = 0;
          for (k = 0; k < N; k++) {
              if (solver[i][j][k]) {counter++; id = k;}
          }
          if (counter == 1){                           
              if (answer[i][j] == 0) {
                  newanswer++;
                  setfalse(id, i, j);
                  answer[i][j] = id+1;
              }    
          }
      }
  }
  return newanswer;
}

int solved(){
  int i,j;
  int isSolved;
  isSolved = 1;
  for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {     
          if (!answer[i][j]) return answer[i][j];
      }        
  }
  return isSolved;
}


int solveproblem(){         
  do {
      if (!searchanswer()) if (!findnewtrue()) return 0;
      //printanswer();           
  } while (!solved());
  return 1;
}



int main ( int argc, char ** argv ) { 
  initsolver();
  readproblem();
  if (solveproblem()) {
      printanswer();
      printf("Solved!!\n");
  }
  else {
      printanswer();
      printf("Stuck!!\n");
  }
}

My wife says that I am ruining the fun of Sudoku. Well, she was right. After that, we rarely play Sudoku together anymore.

Comments