Example #1
0
bool SolveSudoku(int grid[][N])
{
   int row, col;

   if (!FindUnassignedLocation(grid, row, col))
   {
      return true;   // solved it
   }

   for (int num = 1; num <= 9; ++num)
   {
      if (IsSafe(grid, row, col, num))
      {
         grid[row][col] = num;

         if (SolveSudoku(grid))
         { 
            return true;
         }
         else
         {
            grid[row][col] = 0;
         }         
      }
   }
   return false;
}
/* Takes a partially filled-in grid and attempts to assign values to
  all unassigned locations in such a way to meet the requirements
  for Sudoku solution (non-duplication across rows, columns, and boxes) */
bool SolveSudoku(int grid[N][N])
{
    int row, col;
 
    // If there is no unassigned location, we are done
    if (!FindUnassignedLocation(grid, row, col))
       return true; // success!
 
    // consider digits 1 to 9
    for (int num = 1; num <= 9; num++)
    {
        // if looks promising
        if (isSafe(grid, row, col, num))
        {
            // make tentative assignment
            grid[row][col] = num;
 
            // return, if success, yay!
            if (SolveSudoku(grid))
                return true;
 
            // failure, unmake & try again
            grid[row][col] = UNASSIGNED;
        }
    }
    return false; // this triggers backtracking
}
Example #3
0
///\param puzzle The respective puzzle
///\param file Data file
int SolveSudoku(int puzzle[SizeOfPuzzle][SizeOfPuzzle], FILE *fileName){
    int row,col;
    int num;
        /**  If there is no unassigned location, we are done */
        if(FindUnassigned(puzzle,&row,&col) == 0 ){
           solution++;
           printf("\t\t Solution no. %d found \n",solution);
           if(solution < numberOfSolutions){
                printPuzzleInFile(fileName, puzzle, SizeOfPuzzle);
                if(puzzle[0][0] > SizeOfPuzzle){
                    exit(1);
                }
           }
           else{
                printf("\t\t Task solved ! Check solutions file !");
                return 1;
                Sleep(5000);
           }
        }

        iterations = 0; ///\brief Starting counter of iteraions

        /**  Consider digits 1 to SizeOfPuzzle */
        for(num = 1;num <= SizeOfPuzzle;num++){
            iterations++;
            if(iterations < 5000){
                /**  If looks promising */
                if(isSafe(puzzle,row,col,num) == 1){

                    /**  Make tentative assignment */
                    puzzle[row][col] =  num;

                    /**  Return, if success */
                    if(SolveSudoku(puzzle, fileName) == 1){
                        return 1;
                    }
                    /**  Failure, unmake & try again */
                    puzzle[row][col] = UNASSIGNED;

                }
            }
            else{
                printf("\t\t No enought iterations for soltion no. %d",solution);
                fclose(fileName);
                Sleep(5000);
                main();
            }
        }


    return 0; /**  This triggers backtracking */
}
Example #4
0
bool SolveSudoku(int grid[N][N])
{
    int row, col;
    if (!FindUnassignedLocation(grid, row, col))
       return true;
    for (int num = 1; num <= 9; num++)
    {
        if (isSafe(grid, row, col, num))
        {
            grid[row][col] = num;
            if (SolveSudoku(grid))
                return true;
            grid[row][col] = UNASSIGNED;
        }
    }
    return false;
}
Example #5
0
int main(int argc, char * argv[])
{
	int i,j; int grid[9][9];
    if(argc ==1 || strlen(argv[1]) != 81) {
        printf("-1" );
        return 1;
    }
    for(i=0; i<81; i++){
    	grid[i/9][i%9] = argv[1][i]-'0';
        //grid[i/9][i%9] = 0;
    }
    if ( SolveSudoku(grid) == true )
          printGrid(grid);
    else
         printf("-1");
    return 0;
}
int main()
{
    int grid[N][N] = {{3, 0, 6, 5, 0, 8, 4, 0, 0},
                      {5, 2, 0, 0, 0, 0, 0, 0, 0},
                      {0, 8, 7, 0, 0, 0, 0, 3, 1},
                      {0, 0, 3, 0, 1, 0, 0, 8, 0},
                      {9, 0, 0, 8, 6, 3, 0, 0, 5},
                      {0, 5, 0, 0, 9, 0, 6, 0, 0},
                      {1, 3, 0, 0, 0, 0, 2, 5, 0},
                      {0, 0, 0, 0, 0, 0, 0, 7, 4},
                      {0, 0, 5, 2, 0, 6, 3, 0, 0}};
    if (SolveSudoku(grid) == true)
          printGrid(grid);
    else
         printf("No solution exists");

    return 0;
}
Example #7
0
int main()
{
    // 0 means unassigned cells
    // int grid[N][N] = {{3, 0, 6, 5, 0, 8, 4, 0, 0},
    //                   {5, 2, 0, 0, 0, 0, 0, 0, 0},
    //                   {0, 8, 7, 0, 0, 0, 0, 3, 1},
    //                   {0, 0, 3, 0, 1, 0, 0, 8, 0},
    //                   {9, 0, 0, 8, 6, 3, 0, 0, 5},
    //                   {0, 5, 0, 0, 9, 0, 6, 0, 0},
    //                   {1, 3, 0, 0, 0, 0, 2, 5, 0},
    //                   {0, 0, 0, 0, 0, 0, 0, 7, 4},
    //                   {0, 0, 5, 2, 0, 6, 3, 0, 0}};

   // int grid[N][N] =   {{0, 0, 0, 0, 0, 0, 0, 1, 2},     // Skeina Fig7.2
   //                    {0, 0, 0, 0, 3, 5, 0, 0, 0},
   //                    {0, 0, 0, 6, 0, 0, 0, 7, 0},
   //                    {7, 0, 0, 0, 0, 0, 3, 0, 0},
   //                    {0, 0, 0, 4, 0, 0, 8, 0, 0},
   //                    {1, 0, 0, 0, 0, 0, 0, 0, 0},
   //                    {0, 0, 0, 1, 2, 0, 0, 0, 0},
   //                    {0, 8, 0, 0, 0, 0, 0, 4, 0},
   //                    {0, 5, 0, 0, 0, 0, 6, 0, 0}};



   int grid[N][N] =  {{6, 0, 0, 0, 0, 0, 0, 4, 0},   // http://www.sudoku.ws/extreme-20.htm
                      {0, 0, 5, 0, 0, 2, 0, 0, 7},
                      {7, 2, 9, 0, 0, 0, 0, 0, 3},
                      {0, 9, 0, 0, 4, 0, 0, 0, 1},
                      {0, 0, 0, 0, 6, 0, 0, 0, 0},
                      {4, 0, 0, 0, 8, 0, 0, 7, 0},
                      {3, 0, 0, 0, 0, 0, 1, 6, 5},
                      {2, 0, 0, 4, 0, 0, 8, 0, 0},
                      {0, 5, 0, 0, 0, 0, 0, 0, 4}};

    if (SolveSudoku(grid) == true)
          printGrid(grid);
    else
         printf("No solution exists");
 
    return 0;
}
Example #8
0
int SolveSudoku(int grid[N][N])
{
    int row, col;
    if (!FindUnassignedLocation(grid, &row, &col))
       return true; 
 
    long num[N] = {0,0,0,0,0,0,0,0,0};
    setCandidates(&num);

    for (int i = 0; i <= 8; i++)
    {
        if (isSafe(grid, row, col, num[i]))
        {
            grid[row][col] = num[i];

            if (SolveSudoku(grid))
                return true;
 
            grid[row][col] = UNASSIGNED;
        }
    }
    return false; 
}
Example #9
0
void generateSudoku(int grid[N][N]) {
  startingGrid(grid);
  SolveSudoku(grid);
}