Ronzii's Blog

Just your average geek's blog

Validate Sudoku

bool validateSudoku(int a[][9])
{
    int sum=0,product=1;
    // Check Rows
    for(int i=0; i<9; i++)
    {
        {
            sum=0,product=1;
            for(int j=0; j<9; j++)
            {
                sum|=1<<(a[i][j]-1);
                product*=a[i][j];
            }
            if(sum!=511 && product!=362880) return false;
        }

    }
    // Check Column
    for(int i=0; i<9; i++)
    {
        sum=0,product=1;
        for(int j=0; j<9; j++)
        {
            sum|=1<<(a[j][i]-1);
            product*=a[j][i];
        }
        if(sum!=511 && product!=362880) return false;
    }
    // Check grid
    for(int row=0; row<9; row+=3)
    {
        for(int col=0; col<9; col+=3)
        {
            sum=0,product=1;
            for(int i=row; i<row+3; i++)
            {
                for(int j=col; j<col+3; j++)
                {
                    for(int j=col; j<col+3; j++)
                    {
                        sum|=1<<(a[i][j]-1);
                        product*=a[i][j];
                    }
                }
                if(sum!=511 && product!=362880) return false;
            }
        }
    }
    return true;
}
Advertisements

March 20, 2012 - Posted by | C++ | , , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s