矩阵行列变换生成伪数独
/**
*
* @author tiger
* @date 2010-5-28 18:54 于公司
*
*
* 注: 先随机生成最中间的3*3矩阵
* 对这个小矩阵进行适当的行变换和列变换
* 把生成的新矩阵赋值到9*9数独矩阵的适当位置。
* 即可生成一个数独。
*
* 不过,既然是按照一定的行列变换来生成的。
* 有了确定的规律,所以生成的数独看上去也就是有规律的。
* 算是个最简单的数独吧,适合1-2年级的小学生玩。
*/
public class sodu2 {
public sodu2(){
this.mix(array);
this.initCenter();
this.initOther();
this.compose();
//这里是一些混淆化的操作
// this.exchangeC(sodu, 0, 3, 0, 5);
// this.exchangeC(sodu, 1, 7, 0, 5);
// this.exchangeC(sodu, 2, 5, 6, 8);
//
// this.exchangeR(sodu, 0, 4, 0, 2);
// this.exchangeR(sodu, 1, 7, 3, 5);
// print();
}
private int[] array = {1,2,3,4,5,6,7,8,9};
private void mix(int[] array)
{
for (int i = 0; i < array.length; i++)
{
int rand = (int) (Math.random() * 9);
int middle = array[i];
array[i] = array[rand];
array[rand] = middle;
}
}
/**
* 矩阵相乘
* @param a
* @param b
* @return
*/
private int[][] multiply(int[][] a, int b[][])
{
int row = a.length;
int col = b[0].length;
int[][] c = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
for (int k = 0; k < a[i].length; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
private int[][] center = new int[3][3];
private void initCenter()
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
center[i][j] = array[i * 3 + j];
}
}
}
private int[][] up = new int[3][3];
private int[][] upleft = new int[3][3];
private int[][] upright = new int[3][3];
private int[][] down = new int[3][3];
private int[][] downleft = new int[3][3];
private int[][] downright = new int[3][3];
private int[][] left = new int[3][3];
private int[][] right = new int[3][3];
private int[][] juzhen1 = {{0,1,0},{0,0,1},{1,0,0}};
private int[][] juzhen2 = {{0,0,1},{1,0,0},{0,1,0}};
/**
* 初始化周围小矩阵
*/
private void initOther(){
left = multiply( juzhen1, center);
right = multiply( juzhen2, center);
up = multiply(center, juzhen2);
down = multiply(center, juzhen1);
upleft = multiply( juzhen1, up);
upright = multiply( juzhen2, up);
downleft = multiply( juzhen1, down);
downright = multiply( juzhen2, down);
}
private int[][] sodu = new int[9][9];
/**
* 合成到sodu矩阵
*/
private void compose()
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sodu[i][j] = upleft[i][j];
sodu[3+i][j] = left[i][j];
sodu[6+i][j] = downleft[i][j];
sodu[i][3+j] = up[i][j];
sodu[3+i][3+j] = center[i][j];
sodu[6+i][3+j] = down[i][j];
sodu[i][6+j] = upright[i][j];
sodu[3+i][6+j] = right[i][j];
sodu[6+i][6+j] = downright[i][j];
}
}
}
/**
* 列变换
*/
private void exchangeC(int[][] array, int i, int j)
{
int m = 0;
for (int k = 0; k < array.length; k++) {
m = array[k][i];
array[k][i] = array[k][j];
array[k][j] = m;
}
}
/**
* 行变换
*/
private void exchangeR(int[][] array, int i, int j)
{
int m = 0;
for (int k = 0; k < array[0].length; k++) {
m = array[i][k];
array[i][k] = array[j][k];
array[j][k] = m;
}
}
/**
* 列变换
* m必须小于n
*/
private void exchangeC(int[][] array, int i, int j, int m, int n)
{
int w = 0;
for (int k = m; k <=n; k++) {
w = array[k][i];
array[k][i] = array[k][j];
array[k][j] = w;
}
}
/**
* 行变换
* m必须小于n
*/
private void exchangeR(int[][] array, int i, int j, int m, int n)
{
int w = 0;
for (int k = m; k <= n; k++) {
w = array[i][k];
array[i][k] = array[j][k];
array[j][k] = w;
}
}
public void print(){
for (int i = 0; i < sodu.length; i++) {
for (int j = 0; j < sodu[i].length; j++) {
System.out.print(sodu[i][j] + ” , “);
}
System.out.println();
}
}
public static void main(String[] args) {
new sodu2().print();
}
}
/**打印结果如下:
6 , 8 , 2 , 4 , 5 , 9 , 1 , 3 , 7 ,
1 , 3 , 7 , 6 , 8 , 2 , 4 , 5 , 9 ,
4 , 5 , 9 , 1 , 3 , 7 , 6 , 8 , 2 ,
2 , 6 , 8 , 9 , 4 , 5 , 7 , 1 , 3 ,
7 , 1 , 3 , 2 , 6 , 8 , 9 , 4 , 5 ,
9 , 4 , 5 , 7 , 1 , 3 , 2 , 6 , 8 ,
8 , 2 , 6 , 5 , 9 , 4 , 3 , 7 , 1 ,
3 , 7 , 1 , 8 , 2 , 6 , 5 , 9 , 4 ,
5 , 9 , 4 , 3 , 7 , 1 , 8 , 2 , 6 ,
*/