Saturday, June 18, 2011

Sudoku checker


#include<iostream>
#include<conio2.h>

using namespace std;

void inputsudoku(int sdk[10][10])
{
    int a,b;
   
    for (a = 1; a <= 9; a++)
    {
        for(b = 1; b <= 9; b++)
        {
            scanf("%1d",&sdk[a][b]);//input sudoku data
        }
    }
}

int check(int sdk[10][10])
{
    int i, j, k, m, brs_at, klm_kr;
   
    for(i = 1; i <= 9; i++)//control row
    {
        brs_at = (i+2)/3*3-2;
        for(j = 1; j <= 9; j++)//control column
        {
            for(k = 1; k <= 9; k++)
            {
                if (k!=j && sdk[i][j] == sdk[i][k]) return 0;//one row
                if (k!=i && sdk[i][j] == sdk[k][j]) return 0;//one column
            }
            klm_kr = (j + 2)/3*3-2;
            for (k = brs_at; k <= brs_at+2; k++)
            {
                for (m=klm_kr;m<=klm_kr+2;m++)
                {
                    if (sdk[k][m]==sdk[i][j]&&(k!= i || m!=j)) return 0;
                }
            }
        }
        return 1;
    }
}

int main()
{
    int sudoku[10][10];
   
    printf("data sudoku = \n");
    inputsudoku(sudoku);
    if (check(sudoku) != 1) printf("sudoku is wrong");
    else printf("sudoku is right");
    getch();
    return 0;
}

0 comments: