Solving sudoku in C Language

Source: Internet
Author: User

I usually like to play the sudoku game. I suddenly wanted to use a program to automatically solve the problem yesterday. The idea is to trace back and keep testing.

The program code is as follows:


1 # include <stdio. h>
2
3 /*
4 {, 1 },
5 {, 6 },
6 {9, 0, 0 },
7 {1, 2, 5, 0, 6, 0, 3, 4, 7 },
8 {, 6, 0, 0, 9, 8, 5 },
9 {, 9, 0, 2 },
10 {, 0, 0, 4 },
11 {, 9 },
12 {6, 4, 0 }};
13 */
14
15 int data [9] [9] = {
16 {, 0 },
17 {, 7 },
18 {, 0 },
19 {5, 0, 0, 0, 0, 0, 0 },
20 {, 0 },
21 {, 0, 0, 0, 0, 1 },
22 {, 0 },
23 {, 2 },
24 {, 0 }};
25
26 void input ()
27 {
28 int I, j;
29 for (I = 0; I <9; I ++)
30 for (j = 0; j <9; j ++)
31 scanf ("% d", & data [I] [j]);
32}
33
34 void output ()
35 {
36 int I, j;
37 for (I = 0; I <9; I ++)
38 {
39 for (j = 0; j <9; j ++)
40 printf ("% d", data [I] [j]);
41 printf ("\ n ");
42}
43 printf ("\ n ");
44}
45
46/* Check if num can be placed in 3*3 conflict */
47 int CheckSquare (int line, int col, int num)
48 {
49 int I = (line/3) * 3;
50 int j = (col/3) * 3;
51 int m, n;
52 for (m = I; m <I + 3; m ++)
53 for (n = j; n <j + 3; n ++)
54 if (data [m] [n] = num )&&! (M = line & n = col ))
55 return 0;
56 return 1;
57}
58
59/* Check row conflicts */
60 int CheckLine (int line, int col, int num)
61 {
62 int I = 9;
63 while (I --)
64 if (data [line] [I] = num) & (I! = Col ))
65 return 0;
66 return 1;
67}
68
69/* Check column conflicts */
70 int CheckColumn (int line, int col, int num)
71 {
72 int I = 9;
73 while (I --)
74 if (data [I] [col] = num) & (I! = Line ))
75 return 0;
76 return 1;
77}
78
79/* check whether num can be placed in column j of row I */
80 int Check (int I, int j, int num)
81 {
82 return CheckSquare (I, j, num) & CheckLine (I, j, num) & CheckColumn (I, j, num );
83}
84
85/* check whether the task is completed */
86 int IsDone ()
87 {
88 int I, j;
89 for (I = 0; I <9; I ++)
90 for (j = 0; j <9; j ++)
91 if (! Check (I, j, data [I] [j]) | (data [I] [j] = 0 ))
92 return 0;
93 return 1;
94}
95
96 void Calc ()
97 {
98 int I, j, x;
99 if (IsDone ())
100 {
101 output ();
102 return;
103}
104 for (I = 0; I <9; I ++)
105 {
106 for (j = 0; j <9; j ++)
107 {
108 if (data [I] [j] = 0)
109 {
110 for (x = 1; x <= 9; x ++)
111 {
112 if (Check (I, j, x ))
113 {
114 data [I] [j] = x;
115 Calc ();
116}
117}
118 if (x = 10)
119 {
120 data [I] [j] = 0;
121 return;
122}
123}
124}
125}
126}
127
128 int main ()
129 {
130 // input ();
131 Calc ();
132 output ();
133
134 return 0;
135}
In the above program, the number 0 indicates that the position is blank and the number to be filled in.

 

 

From bubble play

Related Article

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.