自己写来玩的,觉得记下来还是有点意义的,呵呵:

1 //--------------------------------//
2 //作者:不喝橙汁
3 //完成日期:09.12.30
4 //--------------------------------//
5
6 public class Suduku_Core
7 {
8 private int[] hole=new int[81];
9 private int[][] test=new int[9][9];
10 private int nHole,N,num,X,Y,FillNum,Cnum;
11
12 //Generator方法
13 //产生原始填充数独
14 private void Generator()
15 {
16 Cnum=0;N=9;nHole=0;
17 int count=0;
18 num=10+(int)(Math.random()*6); //产生10到16之间的随机数,不包括16,也就是10到15的整数
19
20 //while循环
21 //尝试在数组中填入符合数独规则的数字
22 //以此来减少树的分支,提高效率
23
24 while(count!=num)
25 {
26 X=(int)(Math.random()*9);
27 Y=(int)(Math.random()*9);
28 FillNum=1+(int)(Math.random()*9);
29 if(bPack(X,Y,FillNum))
30 {
31 test[X][Y]=FillNum;
32 count++;
33 }
34 }
35 }
36
37 //bPack方法
38 //判断是否可将 n 填入 M[y][x] 里
39 private boolean bPack (int x, int y, int n)
40 {
41 //for循环
42 //纵横判断
43 for (int i=0; i<N; i++) //若 M[0~N-1][x]
44 if (n == test[x][i] || n == test[i][y]) //或 M[y][0~N-1] 中已存在 n
45 return false; //则返回 false
46
47 //for循环
48 //区域判断
49 int D_X,D_Y,OrderNum;
50 OrderNum=9*(x+1)+(y+1);
51 D_X=((int)(x/3))*3;
52 D_Y=((int)(y/3))*3;
53 for(int count=0;count != 9;count++)
54 {
55 if(D_X==x && D_Y==y)
56 continue;
57 if(test[(D_X+count/3)][(D_Y+count%3)]==n)
58 return false;
59 }
60
61 //返回结果
62 return true;
63 }
64
65 //fill方法
66 //此方法用来填充完整已有数子的数独
67 private void fill(int num)
68 {
69 if(Cnum==81)
70 {
71 print();
72 return; //表示填满返回
73 }
74 if(test[Cnum/9][Cnum%9]!=0)
75 {
76 ++Cnum;
77 fill(Cnum); //位置不为0,填充下一个
78 return;
79 }
80 else
81 {
82 //从1到9里面选择数字填进去
83 for(int x=1;x!=11;x++) //x=10 是哨兵
84 {
85 if(Cnum==81)
86 {
87 return; //表示填满返回
88 }
89 if(x==10 && Cnum!=0)
90 {
91 --Cnum;
92 test[(Cnum/9)][(Cnum%9)]=0;
93 return;
94 }
95
96 if(bPack((Cnum/9),(Cnum%9),x))
97 {
98 test[(Cnum/9)][(Cnum%9)]=x;
99 ++Cnum;
100 fill(Cnum);
101 }
102 }
103 }
104
105 }
106
107 //print方法
108 //打印数组到屏幕
109 private void print()
110 {
111 for(int m=0;m<9;m++)
112 {
113 System.out.println();
114 for(int n=0;n<9;n++)
115 {
116 System.out.print(test[m][n]);
117 }
118 }
119 }
120
121 //getArry方法
122 //获取生成的数组
123 public int[][] getArry()
124 {
125 Generator();
126 //print(); //此时打印的是原始的数独数组
127 fill(Cnum);
128 //print(); //此时打印的是填充完整的数独
129 //cout<<endl<<"OVER";
130 System.out.println();
131 System.out.println("OVER"); //打印完毕
132 return test;
133 }
134
135 public static void main(String args[])
136 {
137 Suduku_Core test=new Suduku_Core();
138 test.getArry();
139 }
140 };
141
142
2 //作者:不喝橙汁
3 //完成日期:09.12.30
4 //--------------------------------//
5
6 public class Suduku_Core
7 {
8 private int[] hole=new int[81];
9 private int[][] test=new int[9][9];
10 private int nHole,N,num,X,Y,FillNum,Cnum;
11
12 //Generator方法
13 //产生原始填充数独
14 private void Generator()
15 {
16 Cnum=0;N=9;nHole=0;
17 int count=0;
18 num=10+(int)(Math.random()*6); //产生10到16之间的随机数,不包括16,也就是10到15的整数
19
20 //while循环
21 //尝试在数组中填入符合数独规则的数字
22 //以此来减少树的分支,提高效率
23
24 while(count!=num)
25 {
26 X=(int)(Math.random()*9);
27 Y=(int)(Math.random()*9);
28 FillNum=1+(int)(Math.random()*9);
29 if(bPack(X,Y,FillNum))
30 {
31 test[X][Y]=FillNum;
32 count++;
33 }
34 }
35 }
36
37 //bPack方法
38 //判断是否可将 n 填入 M[y][x] 里
39 private boolean bPack (int x, int y, int n)
40 {
41 //for循环
42 //纵横判断
43 for (int i=0; i<N; i++) //若 M[0~N-1][x]
44 if (n == test[x][i] || n == test[i][y]) //或 M[y][0~N-1] 中已存在 n
45 return false; //则返回 false
46
47 //for循环
48 //区域判断
49 int D_X,D_Y,OrderNum;
50 OrderNum=9*(x+1)+(y+1);
51 D_X=((int)(x/3))*3;
52 D_Y=((int)(y/3))*3;
53 for(int count=0;count != 9;count++)
54 {
55 if(D_X==x && D_Y==y)
56 continue;
57 if(test[(D_X+count/3)][(D_Y+count%3)]==n)
58 return false;
59 }
60
61 //返回结果
62 return true;
63 }
64
65 //fill方法
66 //此方法用来填充完整已有数子的数独
67 private void fill(int num)
68 {
69 if(Cnum==81)
70 {
71 print();
72 return; //表示填满返回
73 }
74 if(test[Cnum/9][Cnum%9]!=0)
75 {
76 ++Cnum;
77 fill(Cnum); //位置不为0,填充下一个
78 return;
79 }
80 else
81 {
82 //从1到9里面选择数字填进去
83 for(int x=1;x!=11;x++) //x=10 是哨兵
84 {
85 if(Cnum==81)
86 {
87 return; //表示填满返回
88 }
89 if(x==10 && Cnum!=0)
90 {
91 --Cnum;
92 test[(Cnum/9)][(Cnum%9)]=0;
93 return;
94 }
95
96 if(bPack((Cnum/9),(Cnum%9),x))
97 {
98 test[(Cnum/9)][(Cnum%9)]=x;
99 ++Cnum;
100 fill(Cnum);
101 }
102 }
103 }
104
105 }
106
107 //print方法
108 //打印数组到屏幕
109 private void print()
110 {
111 for(int m=0;m<9;m++)
112 {
113 System.out.println();
114 for(int n=0;n<9;n++)
115 {
116 System.out.print(test[m][n]);
117 }
118 }
119 }
120
121 //getArry方法
122 //获取生成的数组
123 public int[][] getArry()
124 {
125 Generator();
126 //print(); //此时打印的是原始的数独数组
127 fill(Cnum);
128 //print(); //此时打印的是填充完整的数独
129 //cout<<endl<<"OVER";
130 System.out.println();
131 System.out.println("OVER"); //打印完毕
132 return test;
133 }
134
135 public static void main(String args[])
136 {
137 Suduku_Core test=new Suduku_Core();
138 test.getArry();
139 }
140 };
141
142
虽然是用java写的,但是最重要的还是思想.