An example of Java sudoku problem based on two dimensional arrays

  • 2020-12-16 05:58:24
  • OfStack

This paper gives an example of Java's sudoku problem based on 2-d array. To share for your reference, the details are as follows:

Here, Java2 - dimensional array is used 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 2 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();
      }
    }
  }
  /**
   *  produce 1 a 1-9 The non-repetition length of 9 the 1 Dimensional array 
   */
  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 1 The bit array is: ");
    for (Integer integer : list) {
      System.out.print(" "+integer.toString());
    }
    System.out.println();
    return list;
  }
  /**
   * through 1 Dimensional array and original array generate random Sudoku matrix 
   *
   * traverse 2 The data in the dimension array, in 1 The dimension array finds the position of the current value and puts 1 Dimensional array 
   * Current position plus 1 Is assigned to the current position 2 In the dimension array. The aim is to bring 1 Dimensional array is 
   * According to, in the order that it was randomly generated, take this 9 The data is cyclically exchanged and generated 1 With a 
   * The sudoku matrix of the machine. 
   *
   */
  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(" The 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},
        {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(" The original 2 Dimensional array :");
    Suduku.printArray(seedArray);
    ArrayList<Integer> randomList=Suduku.creatNineRondomArray();
    Suduku.creatSudokuArray(seedArray, randomList);
    System.out.println(" Generate the Sudoku problem :");
    Suduku.creatSudokuQuestion(seedArray);
  }
}

Output:


 The original 2 Dimensional array :
 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 
 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 

 The generated 1 The bit array is: 
 2 3 9 1 6 8 7 5 4
 The processed array 
 1 5 7 9 6 3 8 2 4 
 9 6 3 8 2 4 1 5 7 
 8 2 4 1 5 7 9 6 3 

 5 7 1 6 3 9 2 4 8 
 6 3 9 2 4 8 5 7 1 
 2 4 8 5 7 1 6 3 9 

 7 1 5 3 9 6 4 8 2 
 3 9 6 4 8 2 7 1 5 
 4 8 2 7 1 5 3 9 6 

 Generate the Sudoku problem :
 0 5 7 9 6 3 0 0 0 
 9 6 3 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 8 0 7 1 0 3 9 

 7 1 5 0 0 6 4 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 algorithm, please refer to Java Data Structure and Algorithm Tutorial, Java DOM Node Operation Skills Summary, Java File and Directory Operation Skills Summary and Java Cache Operation Skills Summary.

I hope this article has been helpful in java programming.


Related articles: