Hi Fellas.
I'm in trouble to make a way to solve my sudoku,and thats is my doubt.
I need to solve this Sudoku doing in parts,like,first per section,than per row and last per column.I need to minimize the possibility of answers until i got the right answer for the index.
Code:
#include <stdlib.h>
#include <stdio.h>
//This is for read the initial puzzle.
//The progam asks to someone digit the numbers of points that will be already revealed.
void LeTabuleiroInicial()
{
int iNumDicas;
int iIndex;
int iElemento, iValue;
int iIndexLinha,iIndexColuna;
printf("\nEnter the number of points to be disclosed:");
scanf("%d",&iNumDicas);
// Preparing the array for begin
for (iIndexLinha = 0; iIndexLinha < 81; iIndexLinha++)
{
// The possibility for each element is labeled as 9, this mean that the solution may be any digit [1-9]
iPuzzle[iIndexLinha][0]=9;
// There's no answer revealed
iPuzzle[iIndexLinha][10]=0;
// Declares column of each digit [1-9] as a candidate answer.
for (iIndexColuna = 1; iIndexColuna < 10; iIndexColuna++)
iPuzzle[iIndexLinha][iIndexColuna]=1;
}
//Reading of n elements revealed by the User and their sensitization at the matrix iPuzzle
//The reading consider index of the element varying in the range [1-81] and the value ranging from [1-9].
for (iIndex = 0; iIndex < iNumDicas; iIndex++)
{
printf("Enter in sequence the index of the element and its value:");
scanf("%d %d", &iElemento, &iValue);
//Records 0 in column of all digits ,indicating that there is no answer candidate
for (iIndexColuna = 1; iIndexColuna < 10; iIndexColuna++)
iPuzzle[iElemento-1][iIndexColuna]=0;
//Records that the element has only one possible solution
iPuzzle[iElemento-1][0] = 1;
//Records the revealed solution for the element
iPuzzle[iElemento-1][10] = iValue;
//Records 1 on the colunm of respective revealed digit
iPuzzle[iElemento-1][iValue] = 1;
}
}
void printTabuleiro()
{
int index;
int index2;
printf("\n-------------------------\n|");
for (index = 0; index<81; index++)
{
if (iPuzzle[index][0]==1)
{
for(index2 = 1; index2 <10; index2++)
{
if (iPuzzle[index][index2]==1)
{
printf(" %d", index2);
break;
}
}
}
else printf(" %c", 64 + iPuzzle[index][0] );
if ((index + 1) % 3 == 0)
printf(" |");
if (index + 1 == 81)
printf("\n-------------------------\n");
else if ((index + 1) % 27 == 0)
printf("\n-------------------------\n|");
else if ((index + 1) % 9 == 0)
printf("\n|");
}
}
int resolverSudoku()
{
int i = 1;
do
{
reduzirPossibilidadesPorSecao();//Solving by section
reduzirPossibilidadesPorLinha();//Solving by row
reduzirPossibilidadesPorColuna();//Solving by column
i++;
}
while (resolvido()!=1 && i<150);
if (i>150)
printf("Erro - Apos 150 iteracoes a solucao nao converge.");
}
int main (){
int iPuzzle[81][11];
LeTabuleiroInicial();
printTabuleiro();
resolverSudoku();
printTabuleiro();
return 0;
}
Sorry for my bad english.