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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
#include<iostream.h>
#include<math.h>
/*
// Function to fill up array with all blank spaces (except for element number 26) for testing purpose
void fillarray(int puzzle[81])
{
for(int i=0;i<=80;i++)
{
puzzle[i]=10;
puzzle[25]=6;
}
}
*/
// Function to check if any of the given nine elements are equal or not;
int check9(int ck1,int ck2,int ck3,int ck4,int ck5,int ck6,int ck7,int ck8,int ck9)
{
int fnar[9]={ck1,ck2,ck3,ck4,ck5,ck6,ck7,ck8,ck9};
for(int oc=0;oc<=8;oc++)
{
for(int j=(oc+1);j<9;j++)
{
if(fnar[oc]==fnar[j])
{
goto brkfn;
}
}
}
return 1;
brkfn: return 0;
}
void main()
{
int x; //Will be used as general counter for I/p and O/p operations
int ch; //Will be used for user to choose options after puzzle I/p
int puzzle[81],soln[81];//Puzzle and solution arrays
int add[50],val[50]; //Array to store address of known values from the puzzle and the known value at the address
int c1=0,c2=0,v1=0,v2=0;//Miscellaneous counters and variables
int t=0; //Flag is set to zero
// Code to fill up array
st:
cout<<"Please enter every element of the sudoku puzzle starting from the first row, going from left to the right and then to the next row : \n";
cout<<"\nAll the blank values must be denoted by the number \"10\" \n\n\n";
for(x=0;x<=80;x++)
{
cout<<"Enter element number "<<(x+1)<<" ";
cin>>puzzle[x];
cout<<endl;
}
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
// fillarray(puzzle); //<< Uncomment code to fill array using the above function for testing
//Here, the address of the known values and the known values are collected
for(x=0;x<=80;x++)
{
if(puzzle[x]!=10)
{
add[c1]=x;
val[c1]=puzzle[x];
c1++; // NOTE : Number of known values is (c1+1)
}
}
// Code to display the array just entered in a 9x9 format
for(x=0;x<=80;x++)
{
if(puzzle[x]!=10) // To display the 10s in the array as blank characters
{
cout<<puzzle[x];
}
else
{
cout<<" ";
}
cout<<"|";
if((x+1)%3==0)
cout<<"|";
if((x+1)%27==0)
cout<<"\n---------------------";
if((x+1)%9==0)
cout<<endl;
}
goto c;
edit:
{
//code to edit an element
}
//Code to confirm the puzzle input's correctness
c: cout<<"\nPlease enter one out of the following options : ";
cout<<"\n1. Confirm entered puzzle";
cout<<"\n2. Edit a cell in the puzzle";
cout<<"\n3. Enter the entire puzzle again \n";
cin>>ch;
switch(ch)
{
case 1:
{
goto proc;
}
case 2:
{
goto edit;
}
case 3:
{
goto st;
}
}
cout<<"\nInvalid option.\nPlease enter a correct option\n";
goto c;
// Now starting from 0, going to an array which satisfies our conditions
proc : long double ctr; // Using a long double counter which is actually going to be the solution array in number
for(ctr=(1.0*pow(10,80));ctr<=(9.9*pow(10,80));ctr++)
{
// Here every number is converted into an array
{
long int j; // Internal long int counter
for(j=80;j>=0;j--)
{
soln[j]=int((int)(ctr)%10);
ctr/=10;
}
}
// And every arrayed-number is checked for satisfying our blank puzzle
{
// Check # 1, All known matching elements are checked.
for(x=0;x<=c1;x++)
{
v1=val[x];
v2=add[x];
if(soln[v2]==v1)
{
t=1;
}
else
{
goto next_number;
}
}
//Check #2 for rows and columns and 3x3 cells
// Column 1
if(check9(soln[0],soln[9],soln[18],soln[27],soln[36],soln[45],soln[54],soln[63],soln[72])==1)
{
t++;
}
// Column 2
if(check9(soln[1],soln[10],soln[19],soln[28],soln[37],soln[46],soln[55],soln[64],soln[73])==1)
{
t++;
}
// Column 3
if(check9(soln[2],soln[11],soln[20],soln[29],soln[38],soln[47],soln[56],soln[65],soln[74])==1)
{
t++;
}
// Column 4
if(check9(soln[3],soln[12],soln[21],soln[30],soln[39],soln[48],soln[57],soln[66],soln[75])==1)
{
t++;
}
// Column 5
if(check9(soln[4],soln[13],soln[22],soln[31],soln[40],soln[49],soln[58],soln[67],soln[76])==1)
{
t++;
}
// Column 6
if(check9(soln[5],soln[14],soln[23],soln[32],soln[41],soln[50],soln[59],soln[68],soln[77])==1)
{
t++;
}
// Column 7
if(check9(soln[6],soln[15],soln[24],soln[33],soln[42],soln[51],soln[60],soln[69],soln[78])==1)
{
t++;
}
// Column 8
if(check9(soln[7],soln[16],soln[25],soln[34],soln[43],soln[52],soln[61],soln[70],soln[79])==1)
{
t++;
}
// Column 9
if(check9(soln[8],soln[17],soln[26],soln[35],soln[44],soln[53],soln[62],soln[71],soln[80])==1)
{
t++;
}
// Row 1
if(check9(soln[0],soln[1],soln[2],soln[3],soln[4],soln[5],soln[6],soln[7],soln[8])==1)
{
t++;
}
// Row 2
if(check9(soln[9],soln[10],soln[11],soln[12],soln[13],soln[14],soln[15],soln[16],soln[17])==1)
{
t++;
}
// Row 3
if(check9(soln[18],soln[19],soln[20],soln[21],soln[22],soln[23],soln[24],soln[25],soln[26])==1)
{
t++;
}
// Row 4
if(check9(soln[27],soln[28],soln[29],soln[30],soln[31],soln[32],soln[33],soln[34],soln[35])==1)
{
t++;
}
// Row 5
if(check9(soln[36],soln[37],soln[38],soln[39],soln[40],soln[41],soln[42],soln[43],soln[44])==1)
{
t++;
}
// Row 6
if(check9(soln[45],soln[46],soln[47],soln[48],soln[49],soln[50],soln[51],soln[52],soln[53])==1)
{
t++;
}
// Row 7
if(check9(soln[54],soln[55],soln[56],soln[57],soln[58],soln[59],soln[60],soln[61],soln[62])==1)
{
t++;
}
// Row 8
if(check9(soln[63],soln[64],soln[65],soln[66],soln[67],soln[68],soln[69],soln[70],soln[71])==1)
{
t++;
}
// Row 9
if(check9(soln[72],soln[73],soln[74],soln[75],soln[76],soln[77],soln[78],soln[79],soln[80])==1)
{
t++;
}
// 3x3 cell 1
if(check9(soln[0],soln[1],soln[2],soln[9],soln[10],soln[11],soln[18],soln[19],soln[20])==1)
{
t++;
}
// 3x3 cell 2
if(check9(soln[3],soln[4],soln[5],soln[12],soln[13],soln[14],soln[21],soln[22],soln[23])==1)
{
t++;
}
// 3x3 cell 3
if(check9(soln[6],soln[7],soln[8],soln[15],soln[16],soln[17],soln[24],soln[25],soln[26])==1)
{
t++;
}
// 3x3 cell 4
if(check9(soln[27],soln[28],soln[29],soln[36],soln[37],soln[38],soln[45],soln[46],soln[47])==1)
{
t++;
}
// 3x3 cell 5
if(check9(soln[30],soln[31],soln[32],soln[39],soln[40],soln[41],soln[48],soln[49],soln[50])==1)
{
t++;
}
// 3x3 cell 6
if(check9(soln[33],soln[34],soln[35],soln[42],soln[43],soln[44],soln[51],soln[52],soln[53])==1)
{
t++;
}
// 3x3 cell 7
if(check9(soln[54],soln[55],soln[56],soln[63],soln[64],soln[65],soln[72],soln[73],soln[74])==1)
{
t++;
}
// 3x3 cell 8
if(check9(soln[57],soln[58],soln[59],soln[66],soln[67],soln[68],soln[75],soln[76],soln[77])==1)
{
t++;
}
// 3x3 cell 9
if(check9(soln[60],soln[61],soln[62],soln[69],soln[70],soln[71],soln[78],soln[79],soln[80])==1)
{
t++;
}
next_number:;
}
if(t==28)
{
goto finish;
}
}
finish:cout<<"\n\n\n\n\n";
cout<<"The solution to your sudoku puzzle is :\n\n";
for(x=0;x<=80;x++)
{
cout<<soln[x]<<" ";
if((x+1)%9==0)
cout<<endl;
}
cout<<"\n\n\n\n\n \n";
}
|