Sudoku Solver (Brutish way)

Hi guys! I wrote a program in C++ to solve Sodoku. I just cant figure whats wrong with it.......

I've written //comments everywhere for understanding the code easier.

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

#include <iostream>
using namespace std;

int main() {
	//Initiallizing the variables
	int board[9][9][10];
	int x, y, n;

	//Initiallizing the board
	for (int i=0;i<9;i++) {
		for (int j=0;j<9;j++) {
			for (int k=0;k<10;k++) {
				board[i][j][k] = k;
			}
		}
	}
	//In the array k[x][y][z], z is the possibles values of the square. We take k[x][y][0] as the entered
	//number. If k[x][y][0] == 0, then its a blank.	
	
	//Getting the numbers

	cout<<"**** Enter 0 for a blank in the Sudoku "<<endl<<endl;
	 for ( x = 0; x < 9; x++ ) {
 		for ( y = 0; y < 9; y++ ) {
 			do {
 				cout<<"Enter the value of square number "<<x + 1<<" , "<<y + 1<<" : ";
 				cin>>board[x][y][0];
 			} while (board[x][y][0]>10 );
		}
	} 	
	
	//Seting all extra variables to zero and defining a few new ones that will be used later.
	x = 0;
	y = 0;
	n = 0;
    //	k = 0;
	int i = 0;
	int j = 0;
	int size = 0;
	int o;
	int count1, count2;
	int temp;
	int z;
	int q,k,l;	
	
	//ALGORITHIM BEGINS ------------------------------------------------------------------------------------------------------------------------------														
	
	//Now to loop onto infinity until all the boxes get filled.
	do {
	
	//									----	part.1 - TERMINATING THE VALUES  IN EACH ROW, COLOUMN AND BOX   ----
	
	//Initializing hte conters
	count1 = 0;
	count2 = 0;
	
	//Terminiating possibilities in each row.
	for (i=0;i<9;i++) {
			for (j=0;j<9;j++) {
				for (n=1;n<9;n++) {
					//--
					if (board[i][j][0] == n) {
						//--
							for (k=0;k<9;k++) {
								for (l=0;l<10;l++) {
									if (board[i][k][l] == n) board[i][k][l] = 0;
								}
						}
						//--
					} 
					//--
				}
			}
	}
	
	//Terminiating possibilities in each column.
	for (j=0;j<9;j++) {
			for (i=0;i<9;i++) {
				for (n=1;n<9;n++) {
					//--
					if (board[i][j][0] == n) {
						//--
						for (q=0;q<9;q++) {
								for (l=0;l<10;l++) {
									if (board[q][j][l] == n) board[q][j][l] = 0;
								}
						//--
						}
					} 
					//--
				}
			}
	}
	
	//Terminiating possibilities in each box.
	/*
	The box pattern is shown below :
	
			1 | 2 | 3
			4 | 5 | 6
			7 | 8 | 9
	*/
	
	
	//Box no.1
	for (i=0;i<3;i++) {
			for (j=0;j<3;j++) {
				for (n=1;n<10;n++) {
					//--
						if (board[i][j][0] == n) {
							//--
							for (q=0;q<3;q++) {
								for (k=0;k<3;k++) {
									for (l=0;l<10;l++) {
										if (board[q][k][l] == n) board[q][k][l] = 0;
									}
								}
							}
							//--
						} 
					//--	
				}
	}
			}			
	
	//Box no.2
		for (i=0;i<3;i++) {
			for (j=3;j<6;j++) {
				for (n=1;n<10;n++) {
					//--
						if (board[i][j][0] == n) {
							for (q=0;q<3;q++) {
								for (k=3;k<6;k++) {
									for (l=0;l<10;l++) {
										if (board[q][k][l] == n) board[q][k][l] = 0;
									}
								}
							}
						} 
					//--
				}
			}	
	}
	
	//Box no.3
		for (i=0;i<3;i++) {
			for (j=6;j<9;j++) {
				for (n=1;n<10;n++) {
					//--
						if (board[i][j][0] == n) {
							for (q=0;q<3;q++) {
								for (k=6;k<9;k++) {
									for (l=0;l<10;l++) {
										if (board[q][k][l] == n) board[q][k][l] = 0;
									}
								}
							}
						} 
					//--
				}
			}	
	}
	
	//Box no.4
		for (i=3;i<6;i++) {
			for (j=0;j<3;j++) {
				for (n=1;n<10;n++) {
					//--
						if (board[i][j][0] == n) {
							for (q=3;q<6;q++) {
								for (k=0;k<3;k++) {
									for (l=0;l<10;l++) {
										if (board[q][k][l] == n) board[q][k][l] = 0;
									}
								}
							}
						} 
					//--
				}
			}	
	}
	
	//Box no.5
		for (i=3;i<6;i++) {
			for (j=3;j<6;j++) {
				for (n=1;n<10;n++) {
					//--
						if (board[i][j][0] == n) {
							for (q=3;q<6;q++) {
								for (k=3;k<6;k++) {
									for (l=0;l<10;l++) {
										if (board[q][k][l] == n) board[q][k][l] = 0;
									}
								}
							}
						} 
					//--
				}
			}
	}
	
	//Box no.6
		for (i=3;i<6;i++) {
			for (j=6;j<9;j++) {
				for (n=1;n<10;n++) {
					//--
						if (board[i][j][0] == n) {
							for (q=3;q<6;q++) {
								for (k=6;k<9;k++) {
									for (l=0;l<10;l++) {
										if (board[q][k][l] == n) board[q][k][l] = 0;
									}
								}
							}
						} 
					//--
				}
			}	
	}
	
	//Box no.7
		for (i=6;i<9;i++) {
			for (j=0;j<3;j++) {
				for (n=1;n<10;n++) {
					//--
						if (board[i][j][0] == n) {
							for (q=6;q<9;q++) {
								for (k=0;k<3;k++) {
									for (l=0;l<10;l++) {
										if (board[q][k][l] == n) board[q][k][l] = 0;
									}
								}
							}
						} 
					//--
				}
			}
	}
	
	//Box no.8
		for (i=6;i<9;i++) {
			for (j=3;j<6;j++) {
				for (n=1;n<10;n++) {
					//--
						if (board[i][j][0] == n) {
							for (q=6;q<9;q++) {
								for (k=3;k<6;k++) {
									for (l=0;l<10;l++) {
										if (board[q][k][l] == n) board[q][k][l] = 0;
									}
								}
							}
						} 
					//--
				}
			}
			
	}
	
	//Box no.9
		for (i=6;i<9;i++) {
			for (j=6;j<10;j++) {
				for (n=1;n<9;n++) {
					//--
						if (board[i][j][0] == n) {
							for (q=6;q<9;q++) {
								for (k=6;k<9;k++) {
									for (l=0;l<10;l++) {
										if (board[q][k][l] == n) board[q][k][l] = 0;
									}
								}
							}
						} 
					//--
				}
			}	
	}
	
	//									----	part.2 - ASSIGNING FINAL VALUES TO THE BOX AND DISPLAYING THE BOARD    ----
	
	 for ( x = 0; x < 9; x++ ) {
 		for ( y = 0; y < 9; y++ ) {
 			 for (z = 0; z < 10; z++) {
 			 	if (board[x][y][z] == 0) { count1++; }
 			 	else {temp = board[x][y][z];}
 			if(count1==8) { board[x][y][0] = temp; }    //CHANGE
 			}
		}
	}
	
	system ("CLS");
	
	 for ( x = 0; x < 9; x++ ) {
 		for ( y = 0; y < 9; y++ ) {
 			if (board[x][y][0] == 0) {count2++;}
		}
	}
	
	//Displaying the board 
	cout<<endl<<endl;
	for (i=0;i<9;i++) {
		for (j=0;j<9;j++) {
			cout << "  " << board[i][j][0] << "  " << "|";
		}
		cout<<endl;
		cout<< "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" <<endl;
	}
// --	
	
}while(count2 != 0);


	cout<<endl<<endl<<endl;
	cout<<"		---------------------------------------------"<<endl<<"		THIS SUDOKO SOLVER IS MADE BY EQUINOX"<<endl<<"		---------------------------------------------"<<endl<<endl<<endl;
	return 0;
	system ("PAUSE");
}


Thanks in advance!
I just cant figure whats wrong with it.......


What makes you think something is wrong with it?
I think you need to set count1=0 before line 284. Otherwise each time through the loops at 282 and 283 it will use the previous value.

Same comment for count2.
Topic archived. No new replies allowed.