Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csd.uoc.gr.A14;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.stream.IntStream;
- class Sudoku {
- private static final int BOARD_SIZE = 9;
- private static final int SUBSECTION_SIZE = 3;
- private static final int BOARD_START_INDEX = 0;
- private static final int NO_VALUE = 0;
- private static final int MIN_VALUE = 1;
- private static final int MAX_VALUE = 9;
- private static int[][] board = {
- {8, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 3, 6, 0, 0, 0, 0, 0},
- {0, 7, 0, 0, 9, 0, 2, 0, 0},
- {0, 5, 0, 0, 0, 7, 0, 0, 0},
- {0, 0, 0, 0, 4, 5, 7, 0, 0},
- {0, 0, 0, 1, 0, 0, 0, 3, 0},
- {0, 0, 1, 0, 0, 0, 0, 6, 8},
- {0, 0, 8, 5, 0, 0, 0, 1, 0},
- {0, 9, 0, 0, 0, 0, 4, 0, 6}
- };
- public static void main (String[]args){
- Sudoku solver = new Sudoku();
- solver.solve(board);
- solver.printBoard();
- NewSudoku();
- }
- private void printBoard () {
- for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
- for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
- System.out.print(board[row][column] + " ");
- }
- System.out.println();
- }
- }
- private static boolean solve ( int[][] board){
- for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
- for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
- if (board[row][column] == NO_VALUE) {
- for (int k = MIN_VALUE; k <= MAX_VALUE; k++) {
- board[row][column] = k;
- if (isValid(board, row, column) && solve(board)) {
- return true;
- }
- board[row][column] = NO_VALUE;
- }
- return false;
- }
- }
- }
- return true;
- }
- private static boolean isValid ( int[][] board, int row, int column){
- return rowConstraint(board, row) &&
- columnConstraint(board, column) &&
- subsectionConstraint(board, row, column);
- }
- private static boolean subsectionConstraint ( int[][] board, int row, int column){
- boolean[] constraint = new boolean[BOARD_SIZE];
- int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE;
- int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE;
- int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE;
- int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE;
- for (int r = subsectionRowStart; r < subsectionRowEnd; r++) {
- for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) {
- if (!checkConstraint(board, r, constraint, c)) return false;
- }
- }
- return true;
- }
- private static boolean columnConstraint ( int[][] board, int column){
- boolean[] constraint = new boolean[BOARD_SIZE];
- return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
- .allMatch(row -> checkConstraint(board, row, constraint, column));
- }
- private static boolean rowConstraint ( int[][] board, int row){
- boolean[] constraint = new boolean[BOARD_SIZE];
- return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
- .allMatch(column -> checkConstraint(board, row, constraint, column));
- }
- private static boolean checkConstraint ( int[][] board, int row, boolean[] constraint, int column){
- if (board[row][column] != NO_VALUE) {
- if (!constraint[board[row][column] - 1]) {
- constraint[board[row][column] - 1] = true;
- } else {
- return false;
- }
- }
- return true;
- }
- private static boolean isValidBoard(int[][] brd){
- for (int i=0;i< BOARD_SIZE;i++) {
- for (int j = 0; j < BOARD_SIZE; j++) {
- if (brd[i][j] != 0) {
- for (int k = 0; k < BOARD_SIZE; k++) {
- if (j!=k &&(brd[i][j]==brd[i][k])){
- System.out.println("Found duplicates in rows.");
- return true;
- }
- }
- }
- if (brd [j][i]!=0){
- for (int k=0;k<BOARD_SIZE;k++){
- if (j!=k &&(brd[j][i]==brd[k][i])){
- System.out.println("Found duplicates in columns.");
- return true;
- }
- }
- }
- }
- }
- for (int subcounter =0;subcounter<9;subcounter=subcounter+3) {
- for (int i = subcounter; i < SUBSECTION_SIZE+subcounter; i++) {
- for (int j = subcounter; j < SUBSECTION_SIZE+subcounter; j++) {
- int key = brd[i][j];
- for (int k = subcounter; k < SUBSECTION_SIZE+subcounter; k++) {
- for (int l = subcounter; l < SUBSECTION_SIZE+subcounter; l++) {
- if (((i != k) || (j != l)) && (key == brd[k][l])&&(key!=0)) {
- System.out.println("Found duplicates in a subsection.");
- return true;
- }
- }
- }
- }
- }
- }
- return false ;
- }
- private static boolean isSolvableBoard(int [][]board){
- if (isValidBoard(board)|| !solve(board)){
- System.out.println("Can't solve the board.");
- return false;
- }
- return true;
- }
- public static int [][] BoardGenerator(int X){
- int [][] Xboard =new int [BOARD_SIZE][BOARD_SIZE];
- Random randomGenerator = new Random();
- int randomInt = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
- for (int numRandoms=0;numRandoms<81-X;numRandoms++){//how many random numbers
- int i = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
- int j = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
- while (Xboard[i][j]!=0) {
- i = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;;
- j = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;;
- }
- Xboard[i][j]=randomInt;
- randomInt = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
- }
- return Xboard;
- }
- public static void NewSudoku (){
- System.out.println("Choose the number of the sudoku you want to create:");
- Scanner in = new Scanner(System.in);
- int N = in.nextInt();//number of Sudoku
- if (N<0){
- System.out.println("Wrong input!");
- return;
- }
- int amountSudoku =1;
- int unsolvable =0;
- int valid =0;
- int invalid=0;
- int [][] newBoard = new int [BOARD_SIZE][BOARD_SIZE];
- int [][] newBoardcopy = new int [BOARD_SIZE][BOARD_SIZE];
- System.out.println("Choose the number of the empty cells");
- in = new Scanner(System.in);
- int X = in.nextInt();
- if (X<0||X>81){
- System.out.println("Wrong Input of empty cells !");
- return;
- }
- int i =1;
- long start= System.currentTimeMillis();
- while(i<=N) {//counting until we find N solved boards
- newBoard = BoardGenerator(X) ;
- for (int j=0;j<BOARD_SIZE;j++){
- for (int k=0;k<BOARD_SIZE;k++){
- System.out.println("Board #"+amountSudoku);
- newBoardcopy[j][k]= newBoard[j][k];
- }
- }
- if (isValidBoard(newBoard)){
- invalid++;
- }else{
- for (int j=0;j<BOARD_SIZE;j++){
- for (int k=0;k<BOARD_SIZE;k++){
- System.out.println("Solution of the Board #"+amountSudoku);
- System.out.println(newBoardcopy[j][k]);
- }
- }
- valid++;
- }
- if (isSolvableBoard(newBoardcopy)){
- for (int j=0;j<BOARD_SIZE;j++){
- for (int k=0;k<BOARD_SIZE;k++){
- System.out.println(newBoard[j][k]);
- }
- }
- i++;
- }else{
- unsolvable++;
- }
- amountSudoku ++;
- }
- long end = System.currentTimeMillis();
- float sec = (end -start) / 1000F;
- System.out.println("Empty cells per board :"+X);
- System.out.println("Valid boards created :"+valid);
- System.out.println("Invalid boards created :"+invalid);
- System.out.println("Unsolvable boards created :"+valid);
- System.out.println("Elapsed time in seconds : :"+sec);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement