2

I want to make a program for solving a 3*3 sudoku puzzle. I have made a program but it is only working for 50% problems and for the rest it gives 60% right solution. I do not know how to solve it in a finite number of steps for every possible problem. The technique I have used is that I am searching every individual element of array and check which no does not exist in the same row and column and then I put it in that unit and move to the next. But this is not the solution for every problem. The next thing that come to mind was that we should have to write each and every possible number for a unit and then proceed. But how will we decide that which number we should finally put in the unit. I just want that how to write a solution that will work for every problem. I hope you got the point. The code I have written is

#include<iostream>

using namespace std;

int rowsearch(int l[9][9],int row,int num)    //  function to search a particular number from a row of array 
{       
    int counter=0;     
    for (int c=0 ; c<9 ; c++)
    {
        if (l[row][c]==num)
            counter=counter+1;
    }
    if (counter>0)
        return 1;
    else 
        return 0;
}

int colsearch(int l[9][9],int col,int num)      //    function to search a number from a column of an array 
{
    int counter=0;     
    for (int c=0 ; c<9 ; c++)
    {  
        if (l[c][col]==num)
            counter=counter+1;
    }
    if (counter>0)
        return 1;
    else 
        return 0;
}

int rowcolnotexist(int x[9][9],int  row , int col)  // to find a nuber which does not exists int a row and column
{
    for (int c=1 ; c<=9 ; c++)
    {
        if ( rowsearch(x,row,c)!=1  && colsearch(x,col,c)!=1)
            return c;       
    }
    return 0;                  
}

int main()
{
    int l[9][9]={};

    //  input of the list
    for (int i=0 ; i<9 ; i++)
        for (int j=0 ; j<9 ; j++)
        {
            cout<<"Enter "<<i+1<<"*"<<j+1<<"entry of the list(For not entering a number enter 0).";
            cin>>l[i][j];
        }

    // operations
    for (int i=0 ; i<9 ; i++)
    {
        for (int j=0 ; j<9 ; j++)
            if (l[i][j]==0)
                l[i][j]=rowcolnotexist(l,i,j);
    }

    //  printing list         

    for (int i=0 ; i<9 ; i++)
    {
        for (int j=0 ; j<9 ; j++) 
        {
            cout<<l[i][j];
            if ((j+1)%3==0)
                cout<<"    ";
            else
                cout<<"  ";
        }
        if ((i+1)%3==0)
            cout<<"\n\n\n";
        else 
            cout<<"\n\n";                    
    }
    return 0;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ali Raza
  • 191
  • 2
  • 12
  • 1
    -1 for not enough whitespace – Tom Knapen Nov 08 '13 at 15:17
  • Also, you want to create a solver for 3x3 puzzles, while the code you posted is for 9x9 puzzles. – Tom Knapen Nov 08 '13 at 15:19
  • Please post correct indented code next time. I've fixed it but there are errors in your code! Please check your code! – RvdK Nov 08 '13 at 15:20
  • it has 3*3 boxes and in each box there are 3*3 more elements so the total becomes 9*9 for defining an array – Ali Raza Nov 08 '13 at 15:21
  • @TomKnapen Depends on how you look at it. A 3x3 Sudoku puzzle generally refers to a grid with 3x3 blocks, each block containing 3x3 entries. – anjruu Nov 08 '13 at 15:21
  • I am so sorry for all this. I tried my best to write this but i do not know much to write in stack overflow because i am new. – Ali Raza Nov 08 '13 at 15:22
  • @user2969370 Have you considered searching, say, wikipedia [for a list of algorithms to solve sudoku puzzles](https://en.wikipedia.org/wiki/Sudoku_solving_algorithms)? – anjruu Nov 08 '13 at 15:23
  • yes exactly like that anjruu. – Ali Raza Nov 08 '13 at 15:23
  • yeah i did but it provides many solutions for solving different but i need one which work for all. – Ali Raza Nov 08 '13 at 15:24
  • I don't understand your comment. The link has a list of algorithms for solving Sudoku puzzles programmatically. If you have correctly implemented one of these algorithms, the implementation will be able to solve any Sudoku puzzle that is actually solvable. – anjruu Nov 08 '13 at 15:26
  • Read answers to similar question: http://stackoverflow.com/q/7695926/395718 – Dialecticus Nov 08 '13 at 15:28
  • I think @user2969370 wants to generate all possible 3x3 sudoku solutions –  Nov 08 '13 at 15:35
  • no i did not mean that Arkadly. I want to write a solution that works for every possible 3*3 suduko puzzle. The thing which i do not understand in the link you have given Dialecticus that if we make a sub array for every square i.e a 3d array then we can store all possible values in that vector and if there are different numbers which match with other squares values, then how will i chose that which value to be discarded from which square. – Ali Raza Nov 08 '13 at 15:41

1 Answers1

2

I'd recommend the same algorithm I use when solving them myself ;-)

Choose an arbitrary square and list all the valid values for it, based on what is present in all the other rows (there's probably a way to make a more efficient decision about which square to start with).

Then move on to a related empty square (there's probably a way to make a more efficient decision about which square to check next) and store all its possible values.

Rinse and repeat until you find a square that has only one valid value.

Then unzip.

This should sounds like a recursive problem, by now. (note: almost anything that can be done recursively can be done conventionally, but the idea is the same).

So, store up a list of partially solved squares, and when you get to a square that's been solved completely, go back through your list in reverse order re-evaluating your partial solutions with the new data (namely, the one you WERE able to solve).

Rinse and repeat for full body and volume.

Matt
  • 775
  • 7
  • 24