Java:如何随机化数组列表而不重复

时间:2013-06-11 00:41:32

标签: java arrays algorithm random sudoku

import java.util.Random;

public class Sudoku {
  int[][] SquareNumbers = { 
      { 4, 3, 5, 8, 7, 6, 1, 2, 9 }, { 8, 7, 6, 2, 1, 9, 3, 4, 5 }, { 2, 1, 9, 4, 3, 5, 7, 8, 6 },
      { 5, 2, 3, 6, 4, 7, 8, 9, 1 }, { 9, 8, 1, 5, 2, 3, 4, 6, 7 }, { 6, 4, 7, 9, 8, 1, 2, 5, 3 },
      { 7, 5, 4, 1, 6, 8, 9, 3, 2 }, { 3, 9, 2, 7, 5, 4, 6, 1, 8 }, { 1, 6, 8, 3, 9, 2, 5, 7, 4 } };

  Random Digits = new Random(); // random numbers to exchange Rows
  Random HiddenNumbers = new Random();
  int Grid[][] = new int[9][9];

  public int[][] Generator() {
    for (int x = 0; x < Digits.nextInt(); x++) {
      for (int da = 0; da < 3; da++) {

      }
    }

    return SquareNumbers;
  }

  int[][] Hide() {
    for (int i = 0; i < 9; i++)
      for (int j = 0; j < 9; j++)
        Grid[i][j] = SquareNumbers[i][j];

    int Row, Columns, Concealer;

    Concealer = 55 + Digits.nextInt(1);

    for (int i = 0; i < Concealer; i++) {
      Row = HiddenNumbers.nextInt(9);
      Columns = HiddenNumbers.nextInt(9);
      Grid[Row][Columns] = -1;
    }
    return Grid;
  }

  public int[][] getSquareNumbers() {
    return SquareNumbers;
  }

  public void setSquareNumbers(int[][] SquareNumbers) {
    this.SquareNumbers = SquareNumbers;
  }

  private static Sudoku instance = null;

  protected Sudoku() {
    // Exists only to defeat instantiation.
  }

  public static Sudoku getInstance() {
    if (instance == null) {
      instance = new Sudoku();
    }
    return instance;
  }
}

有没有一种方法可以将它随机化,以便它像Sudoku游戏一样运行而不是双数组列表?因为它在列或行中没有重复,并且每三个较小的网格使用一次数字?

1 个答案:

答案 0 :(得分:1)

生成随机数独游戏的一种方法如下:

  1. 生成满足数独谜题约束的随机谜题
  2. 随机交换数字(例如将2s替换为3s,7s替换为1s等)
  3. 在其3个集合中随机交换列或行(例如,交换第一列和第三列,或第5和第6列)
  4. 随机切换3列或3行的集合 另一组列。
  5. 虽然这些看起来像是不同的谜题,但实际上它们都是各向同性的拉丁方块(即所有等价物,因为它们可以通过重新排序行/列而缩小为相同的谜题)。

    如果您想要更随机的解决方案,可能已在此处回答:https://stackoverflow.com/a/6964044/2471910

相关问题