关闭

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

128人阅读 评论(0) 收藏 举报
分类:
脑筋急转弯一样的东西。。。
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

/**
 *
 * @author Administrator
 */
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]);
                }

            }
        }
    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4919次
    • 积分:306
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类