# 编程之美1.15构造数独---置换法java版

脑筋急转弯一样的东西。。。
/*
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Test;

/**
*
*/
public class SudokuDisplace {

public static void main(String[] args) {
int[][] shudu = new int[9][9];
int[][] arr = {{3, 5, 8}, {7, 9, 4}, {1, 6, 2}};
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
shudu[i][j] = 0;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
shudu[i + 3][j + 3] = arr[i][j];
}
}
//置换行
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 0) {
shudu[i + 3][j] =shudu[i+5][j+3];
shudu[i + 3][j + 6] = shudu[i+4][j+3];
}

if (i == 1) {
shudu[i + 3][j] = shudu[i+2][j+3];
shudu[i + 3][j + 6] = shudu[i+4][j+3];
}

if (i == 2) {
shudu[i + 3][j] = shudu[i+2][j+3];
shudu[i + 3][j + 6] = shudu[i+1][j+3];
}

}
}
//置换列
for(int i=0;i<3;i++){
for(int j =0;j<3;j++){
if(j ==0){
shudu[i][j + 3] = shudu[i+3][j+5];
shudu[i + 6][j + 3] = shudu[i+3][j+4];
shudu[i][j]=shudu[i+3][j+2];
shudu[i+6][j]=shudu[i+3][j+1];
shudu[i][j+6]=shudu[i+3][j+8];
shudu[i+6][j+6]=shudu[i+3][j+7];
}
if (j == 1) {
shudu[i][j + 3] = shudu[i+3][j+2];
shudu[i + 6][j + 3] = shudu[i+3][j+4];
shudu[i][j] = shudu[i+3][j-1];
shudu[i+6][j]=shudu[i+3][j+1];
shudu[i][j+6]=shudu[i+3][j+5];
shudu[i+6][j+6]=shudu[i+3][j+7];
}
if (j == 2) {
shudu[i][j + 3] = shudu[i+3][j+2];
shudu[i + 6][j + 3] = shudu[i+3][j+1];
shudu[i][j] = shudu[i+3][j-1];
shudu[i+6][j]=shudu[i+3][j-2];
shudu[i][j+6]=shudu[i+3][j+5];
shudu[i+6][j+6]=shudu[i+3][j+4];
}
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (j != 8) {
System.out.print(shudu[i][j] + "    ");
} else {
System.out.println(shudu[i][j]);
}

}
}
}

}


• 本文已收录于以下专栏：

举报原因： 您举报文章：编程之美1.15构造数独---置换法java版 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)