Support for command line Sudoku

Source: Internet
Author: User

# Include <stdio. h>
# Include <memory. h>
# Include <stdlib. h>
# Include <string. h>

Typedef struct
{
Int X; // The X coordinate of the array.
Int y; // The ordinate of the input array.
} Elemtype;

Static elemtype list [81]; // defines a tablespace.
Int Len =-1; // Len record table size

Void addlistelem (elemtype );
Int getlistelem (elemtype *, INT );
Void showme (INT [] [9]);
Int addcount (INT [] [9]);
Int checkline (INT [] [9], Int, INT );
Int checkrow (INT [] [9], Int, INT );
Int checkpalace (INT [] [9], Int, Int, INT );

Int getlistelem (elemtype * E, int I)
{// Retrieve Element
If (I <0)
{
Return 0;
}
* E = list [I];
Return 1;
}

Void addlistelem (elemtype E)
{// Store the coordinates originally empty in the table
Len ++;
List [Len] = E;
}

Int checkpalace (INT arr [] [9], int line, int row, int count)
{// Check the palace
Int I, J;
Int A, B;
I = line/3*3;
J = row/3*3; // I, j is the starting vertical subscript of a palace
For (A = I; A <I + 3; ++)
{
For (B = J; B <j + 3; ++ B)
{
If (ARR [a] [B] = count)
{
Return 1;
}
}
}
Return 0;
}

Int checkrow (INT arr [] [9], int row, int count)
{// Check the column
Int I;
For (I = 0; I <9; ++ I)
{
If (ARR [I] [row] = count)
{
Return 1;
}
}
Return 0;
}

Int checkline (INT arr [] [9], int line, int count)
{// Check the row
Int J;
For (j = 0; j <9; ++ J)
{
If (ARR [Line] [J] = count)
{
Return 1;
}
}
Return 0;
}

Int addcount (INT arr [] [9])
{// Add count
Int I, J, K;
Int count;
Elemtype E;

K =-1;
For (I = 0; I <9; ++ I)
{
For (j = 0; j <9 ;)
{
Count = 1;
If (! Arr [I] [J])
{
K ++;
While (checkline (ARR, I, count) | checkrow (ARR, J, count)
| Checkpalace (ARR, I, j, count) & count <= 9)
{
Count ++;
}
If (count> 9)
{
Do
{
Arr [I] [J] = 0;

If (getlistelem (& E, k-1 ))
{
K --;
I = E. X;
J = E. Y;

}
Else
{
Return 0;
}
Count = arr [I] [J] + 1;
While (checkline (ARR, I, count) | checkrow (ARR, J, count)
| Checkpalace (ARR, I, j, count) & count <= 9)
{
Count ++;
}
} While (count> 9 );
Arr [I] [J] = count;
}
Else
{
Arr [I] [J] = count;
J ++;
}
}
Else
{

J ++;
}
}
}
Return 1;
}

Void showme (INT arr [] [9])
{// Display Sudoku
Int I, J;
For (I = 0; I <9; ++ I)
{
If (I + 1) % 3 = 1)
{
Printf ("------------------------/N ");
}
For (j = 0; j <9; ++ J)
{
If (J + 1) % 3 = 1)
{
Printf ("% 2C", '| ');
}
Printf ("% 2D", arr [I] [J]);
}
Printf ("% 2C/N", '| ');
}
Printf ("------------------------/N ");
}

Int main (INT argc, char * argv [])
{
File * out;
Char ch;
Int I, J, K;
Elemtype E;
Int arr [9] [9];
Memset (ARR, 0, 81 * sizeof (INT ));

If (argc = 2 &&! Strcmp ("/? ", Argv [1]) | argc = 1)
{
Printf ("finding the solution of the sudoku/n ");
Printf ("Sudoku data path/n ");
Return 0;
}
If (argc = 3 | argc = 2)
{
K = 0;
For (I = 0; I <9; ++ I)
{
For (j = 0; j <9; ++ J)
{
Ch = argv [1] [k];
Arr [I] [J] = atoi (& Ch );
// Sscanf (& argv [1] [K], "% d", & arr [I] [J]);
K ++;
If (! Arr [I] [J])
{
E. x = I;
E. Y = J;
Addlistelem (E );
}
}
}
If (addcount (ARR ))
{
// Showme (ARR );
}
Else
{
Printf ("this number is unique/N ");
Return-1;
}


If (argc = 3)
{
Out = fopen (argv [2], "");
If (out)
{
For (I = 0; I <9; ++ I)
{
For (j = 0; j <9; ++ J)
{
Fprintf (Out, "% 2D", arr [I] [J]);
}
Fprintf (Out, "/N ");
}
Fprintf (Out, "/N ");
Fclose (out );
}
Return 0;
}

Showme (ARR );
}
Else
{
Printf ("Incorrect format/N ");
Return 1;
}

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.