This article describes the Sudoku problem implemented by Java based on two-dimensional arrays. Share it for your reference, as follows:
Here we use Java two-dimensional arrays to implement the Sudoku problem.
(1) Generate simple sudoku (2) Generate sudoku problem**
Code
import java.util.Random;import java.util.ArrayList;public class Suduku { /** *Print two-dimensional array, sudoku matrix*/ public static void printArray(int a[][]) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { System.out.print(" +a[i][j]); if (0==((j+1)%3)) { System.out.print(" "); } } System.out.println(); if(0==((i+1)%3)) { System.out.println(); } } } } /** * Generate a one-dimensional array of 1-9 with no repeating length of 9*/ public static ArrayList<Integer> creatNineRondomArray() { ArrayList <Integer> list = new ArrayList<Integer>(); Random random=new Random(); for (int i = 0; i < 9; i++) { int randomNum=random.nextInt(9)+1; while (true) { if (!list.contains(randomNum)) { list.add(randomNum); break; } randomNum=random.nextInt(9)+1; } } System.out.println("The generated one-bit array is: "); for (Integer integer : list) { System.out.print("+integer.toString()); } System.out.println(); return list; } /** * Generate a random sudoku matrix through one-dimensional array and the original array* *Transfer the data in the two-dimensional array, find the position of the current value in the one-dimensional array, and assign the one-dimensional array* the current position plus a position value to the current two-dimensional array. The purpose is to use a one-dimensional array as the basis, and to exchange these 9 data in a random order to generate a random sudoku matrix. * */ public static void creatSudokuArray(int[][]seedArray,ArrayList<Integer> randomList) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { for (int k = 0; k < 9; k++) { if(seedArray[i][j]==randomList.get(k)) { seedArray[i][j]=randomList.get((k+1)%9); break; } } } } System.out.println("processed array"); Suduku.printArray(seedArray); } public static void creatSudokuQuestion(int [][] a) { Random rand=new Random(); for(int i=0;i<9;i++){ for(int j=0;j<4;j++){ a[i][(int)rand.nextInt(9)]=0; } } Suduku.printArray(a); } // public static void main(String[] args) { int seedArray[][]={ {9,7,8,3,1,2,6,4,5}, {3,1,2,6,4,5,9,7,8}, {6,4,5,9,7,8,3,1,2}, {7,8,9,1,2,3,4,5,6,7,8,9}, {1,2,3,4,5,6,7,8,9}, {4,5,6,7,8,9,1,2,3}, {8,9,7,2,3,1,5,6,4}, {2,3,1,5,6,4,8,9,7}, {5,6,4,8,9,7,2,3,1} }; System.out.println("Original 2D Array:"); Suduku.printArray(seedArray); ArrayList<Integer> randomList=Suduku.creatNineRondomArray(); Suduku.creatSudokuArray(seedArray, randomList); System.out.println("Generate Sudoku Problem:"); Suduku.creatSudokuQuestion(seedArray); }}Output:
9 8 3 1 2 4 5 6 4 8 9 7 5 6 4 8 9 7 2 3 1 5 6 4 8 9 7 2 3 1 5 6 4 8 9 7 5 6 4 8 9 7 2 3 1 5 6 4 8 9 7 5 6 4 8 9 7 2 3 1 5 6 4 8 9 7 2 3 1 5 6 4 8 9 7 5 6 4 8 9 7 2 3 1 5 7 8 2 4 1 5 7 8 2 4 1 5 7 8 2 4 1 5 7 8 2 4 1 5 7 9 6 3 0 0 0 0 0 0 5 7 0 2 4 1 0 7 9 6 0 5 0 1 6 0 9 2 0 0 6 0 9 2 0 0 5 7 0 2 0 0 6 0 9 2 0 0 5 7 0 2 0 0 6 0 9 2 0 0 5 7 0 2 0 0 5 7 0 2 0 0 6 0 9 2 0 0 5 7 0 2 0 0 5 7 0 2 0 0 6 0 9 2 0 0 5 7 0 2 0 0 5 7 0 2 0 0 6 0 0 5 7 0 2 0 0 6 0 0 5 7 0 2 0 0 6 0 0 6 0 9 2 0 0 5 7 0 2 0 0 5 7 0 2 0 0 6 0 0 6 0 9 2 0 0 5 7 0 2 0 0 5 7 0 2 0 0 6 0 0 6 0 9 2 0 0 5 7 0 2 0 0 5 7 0 2 0 0 6 0 0 6 0 9 2 0 0 5 7 0 2 0 0 5 7 0 2 0 0 5 7 0 2 0 0 6 0 7 0 2 0 0 5 7 0 2 0 0 6 0 7 0 2 0 0 5 7 0 2 0 0 6 0 7 0 2 0 0 5 7 0 2 0 8 2 3 0 6 4 8 2 7 0 5 4 8 2 7 0 0 3 9 6
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.