Program to solve a Sudoku puzzle

#include <stdio.h>
#include <stdlib.h>
#define N     9
#define true  1
#define false 0

void printSudoku(int A[N][N]);
int  solveSudoku(int A[N][N]);
int  FindFreeLocation(int A[N][N], int *row, int *col);
int  isSafe(int A[N][N], int row, int col, int num);

int main(void)
{
    int A[N][N] = { {0,0,0,  0,0,7,  0,5,0},
                    {0,0,6,  0,0,0,  1,0,0},
                    {4,2,0,  0,5,0,  0,7,0},
                                     
                    {0,0,0,  0,0,2,  4,0,9},
                    {0,0,0,  1,8,3,  0,0,0},
                    {7,0,3,  9,0,0,  0,0,0},
                                     
                    {0,5,0,  0,9,0,  0,2,3},
                    {0,0,4,  0,0,0,  8,0,0},
                    {0,3,0,  5,0,0,  0,0,0}};

    //Print the sudoku matrix (before solving)
    printf("--------------- \n");
    printf("Before solving: \n");
    printf("--------------- \n");
    printSudoku(A);
    
    //Solve the sudoku puzzle
    if(!solveSudoku(A))
    {
       printf("No solution exists!\n");
       exit(1);
    }
    
    //Print the sudoku matrix (after solving)
    printf("\n\n");
    printf("-------------- \n");
    printf("After solving: \n");
    printf("-------------- \n");
    printSudoku(A);
        
    return 0;
}

void printSudoku(int A[N][N])
{
    int i,j;
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            if(j==3 || j==6) printf("  ");
            printf("%d ",A[i][j]);
        }
        printf("\n");
        if(i == 2 || i == 5) printf("\n");
    }
}

int solveSudoku(int A[N][N])
{
   int row, col;
   int num;
   
    if(!FindFreeLocation(A,&row,&col))
       return true;
    
    for(num = 1; num <=9; num++) 
    {
        if(isSafe(A,row,col,num))
        {
            // Assign a value and try solving the remaining puzzle
            A[row][col] = num;
            
            if(solveSudoku(A))
               return true;
            
            // Sorry, the previous assignment failed, reset this.
            A[row][col] = 0;
        }
    }
    
    return false;
}

int FindFreeLocation(int A[N][N], int *row, int *col)
{
    int i,j;
    
    for(i=0;i<N;i++)
       for(j=0;j<N;j++)
          if(A[i][j] == 0)
          {
             *row = i;
             *col = j;
             return true;
          }
        
    return false;
}

int isSafe(int A[N][N], int row, int col, int num)
{
     int i,j;
     
     // Check the row
     for(i=0;i<N;i++)
     {
       if(A[row][i] == num)
           return false;
     }

     // Check the column
     for(i=0;i<N;i++)
     {
       if(A[i][col] == num)
           return false;
     }

     // Check the box
     // Set the posisitions of row, col to the beginning of box
     row = row - row%3;
     col = col - col%3;
     
     for(i=0;i<3;i++)
     {
        for(j=0;j<3;j++)
        {
           if(A[i+row][j+col] == num)
              return false;
        }
     }
     
     // It's safe
     return true;
}

No comments:

Post a Comment