java 生成数独

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class UniqueNumberTest {

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            int[][] mat = generateRandomMat();
            // todo 检测是否包含0
            boolean success = isSuccessMat( mat );
            if( success ){
                printMat( mat );
                System.out.println();
                System.out.println();
                System.out.println();
            }else {
                //System.err.println( "生成失败" );
            }
        }
    }

    private static boolean isSuccessMat(int[][] mat) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if( mat[i][j]==0 ){
                    return false;
                }
            }
        }
        return true;
    }

    public static String getSquareNameByRowAndColIndex( int rowIndex,int colIndex){
        // todo
        return rowIndex / 3 + "_" + colIndex / 3;
    }

    public static int[][] generateRandomMat(){
        Random random = new Random();
        int[][] mat={
                        {0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0}
                    };
        for (int rowIndex = 0; rowIndex < 9; rowIndex++) {
            for (int colIndex = 0; colIndex < 9; colIndex++) {
                setValue4Mat( mat,rowIndex,colIndex,random );
                int value_gen = mat[rowIndex][colIndex];
                // System.out.println( "set value " + value_gen + "  for mat at ( " + rowIndex + "," + colIndex + " )" );
            }
        }
        return mat;
    }

    private static void setValue4Mat(int[][] mat, int rowIndex, int colIndex,Random random) {
        List enableValues = init9Number();

        int[] row = mat[rowIndex];
        for (int i = 0; i < 9; i++) {
            String value = String.valueOf(row[i]);
            if( enableValues.contains( value ) ){
                enableValues.remove( value );
            }
        }

        for (int i = 0; i < 9; i++) {
            String value = String.valueOf(mat[i][colIndex]);
            if( enableValues.contains( value ) ){
                enableValues.remove( value );
            }
        }

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                String squareName1 = getSquareNameByRowAndColIndex(i, j);
                String squareName2 = getSquareNameByRowAndColIndex(rowIndex, colIndex);
                if( squareName1.equals( squareName2 ) ){
                    String value = String.valueOf(mat[i][j]);
                    if( enableValues.contains( value ) ){
                        enableValues.remove( value );
                    }
                }
            }
        }
        if( enableValues.size() > 0 ){
            String value = enableValues.get(random.nextInt(enableValues.size()));
            mat[ rowIndex ][ colIndex ] = Integer.valueOf( value );
        }
    }

    private static List init9Number() {
        List numbers = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            numbers.add( String.valueOf( i + 1 ) );
        }
        return numbers;
    }

    public static void printMat( int[][] mat){
        if( mat == null || mat.length != 9 ){
            return;
        }
        int[] row1 = mat[0];
        if( row1 == null || row1.length != 9 ){
            return;
        }
        for (int rowIndex = 0; rowIndex <9 ; rowIndex++) {
            int[] row = mat[rowIndex];
            for (int colIndex = 0; colIndex <9 ; colIndex++) {
                System.out.print( row[ colIndex ] );
                System.out.print( "  " );
                if( colIndex % 3 == 2 ){
                    System.out.print( " " );
                }
            }
            System.out.println();
            if( rowIndex % 3 == 2 ){
                System.out.println();
            }
        }
    }
}

你可能感兴趣的:(算法,数独,java,算法)