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

#include <iostream>
#include <algorithm>
void print (int [][9]);
bool checkvalidity (int [][9]);
bool checkline (int [9]);
int main ()
{
//int sudoku_arr [9][9];
int sudoku_arr_copy [9][9];
//initialise all values to 0
//for (int a =0;a < 9; a++)
// for (int b=0; b < 9; b++)
// sudoku_arr [a][b] = 0;
//entry of values
int sudoku_arr [9][9] = {{0,0,2, 1,4,0 ,8,6,0},
{4,0,8, 2,5,0 ,0,0,0},
{1,7,0, 3,0,0 ,0,0,4},
{0,1,0, 0,0,0 ,0,7,6},
{6,0,5, 7,0,2 ,3,4,1},
{0,9,0, 0,0,0 ,0,2,8},
{5,2,0, 9,0,0 ,0,0,7},
{8,0,6, 5,7,0 ,0,0,0},
{0,0,7, 8,2,0 ,6,3,0}};
//copy sudoku box to copy
for (int a =0;a < 9; a++)
for (int b=0; b < 9; b++)
sudoku_arr_copy [a][b] = sudoku_arr[a][b];
//BRUTE FORCE
for (int pos =0; pos < 81; pos++ )
{
if (sudoku_arr_copy [pos/9][pos%9] != 0)
continue;
bool deadend = false;
while (!deadend)
{
if (sudoku_arr[pos/9][pos%9] == 9)
{
deadend = true;
break;
}
sudoku_arr[pos/9][pos%9]++;
if (!checkvalidity (sudoku_arr))
continue;
if (checkvalidity (sudoku_arr))
break;
}
if (deadend)
{
//set value to zero
sudoku_arr [pos/9][pos%9] = 0;
//find immediately previous FILLABLE entry
for (int backpos = pos  1; backpos >=0 ; backpos)
{
if ( sudoku_arr_copy [backpos/9][backpos%9] ==0)
{
pos = backpos;
break;
}
}
}
}
print (sudoku_arr);
}
//CHECK VALIDITY
bool checkvalidity (int sudoku_arr[][9])
{
int temp_arr [9];
//rows
for (int a=0;a <9;a++)
{
for (int b=0;b <9; b++)
temp_arr[b] = sudoku_arr[a][b];
if (!checkline (temp_arr))
return false;
}
//columns
for (int a=0;a< 9;a++)
{
for (int b=0;b<9;b++)
temp_arr [b] = sudoku_arr [b][a];
if (!checkline (temp_arr))
return false;
}
//mini_boxes
int count =0;
for (int d=0;d <9;d = d+3)
{
for (int a=0;a<9; a= a+3)
{
for (int c = d; c < (d+3);c++)
{
for (int b=a; b < (a +3);b++)
{
temp_arr [count] = sudoku_arr [c][b];
count++;
}
}
//box gathered
count =0;
if (! checkline (temp_arr))
return false;
}
}
//otherwise valid
return true;
}
//
bool checkline (int linearr_copy[9])
{
std::sort (linearr_copy, linearr_copy + 9);
for (int a=0; a<8;a++)
{
if (linearr_copy [a] == 0)
continue;
if (linearr_copy [a] == linearr_copy [a+1])
return false;
}
return true;
}
//PRINT
void print (int sudoku_arr [][9])
{
for (int a =0;a < 9; a++)
{
for (int b=0; b < 9; b++)
{
std::cout << sudoku_arr [a][b];
if (b ==2  b== 5)
std::cout << " ";
}
if (a ==2  a== 5)
std::cout <<std::endl;
std::cout << std::endl;
}
}
 