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

脑筋急转弯一样的东西。。。
/*
 * 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]);
                }

            }
        }
    }

}

阅读更多
个人分类: 编程之美笔记
上一篇编程之美1.15构造数独-----回溯法java版
下一篇编程之美1.1624点游戏解法一java版
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭