gpt4 book ai didi

java - 用范围内的随机数字填充数组

转载 作者:行者123 更新时间:2023-11-30 10:47:51 25 4
gpt4 key购买 nike

我正在编写一个数独棋盘游戏生成器。我的代码如下所示:

/**
* Created by szubansky on 3/9/16.
*/

public class SudokuBoard {

static int N = 9;
static int[][] grid = new int[N][N];

static void printGrid()
{
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++) {
System.out.printf("%5d", grid[row][col]);
}
System.out.println("\n");
}
}

private static boolean checkRow(int row, int num)
{
for( int col = 0; col < 9; col++ )
if(grid[row][col] == num)
return false;

return true;
}

private static boolean checkCol(int col, int num)
{
for( int row = 0; row < 9; row++ )
if(grid[row][col] == num)
return false;

return true;
}

private static boolean checkBox(int row, int col, int num)
{
row = (row / 3) * 3;
col = (col / 3) * 3;

for(int r = 0; r < 3; r++)
for(int c = 0; c < 3; c++)
if(grid[row+r][col+c] == num)
return false;

return true;
}

public static boolean fillBoard(int row, int col, int[][] grid)
{
if(row==9) {
row = 0;
if (++col == 9)
return true;
}

if(grid[row][col] != 0)
return fillBoard(row+1, col, grid);
for(int num=1 + (int)(Math.random() * ((9 - 1) + 1)); num<=9; num++)
{
if(checkRow(row,num) && checkCol(col,num) && checkBox(row,col,num)){
grid[row][col] = num;
if(fillBoard(row+1, col, grid))
return true;
}
}
grid[row][col] = 0;
return false;
}

static public void main(String[] args){
fillBoard(0, 0, grid);
printGrid();
}
}

问题是在 fillBoard 回溯算法中生成数字它基本上到处都是 0当我将 for 循环中的 num 范围更改为 10 时,它进行得很顺利,但我的数字需要低于 9我还可以将回溯 fillBoard 的开头更改为 row==8col==8 并用随机数正确填充它,最后一行和最后一列为“0 ”。如何生成 1 到 9 的随机数并填满我的所有网格?

最佳答案

试试这个:

public static void main(String[] args) {
int[][] grid = new int[9][9];
randomFillGrid(grid, 1, 10);
for (int[] row : grid) {
System.out.println(Arrays.toString(row));
}
}

static void randomFillGrid(int[][] grid, int randomNumberOrigin, int randomNumberBound) {
PrimitiveIterator.OfInt iterator = ThreadLocalRandom.current()
.ints(randomNumberOrigin, randomNumberBound)
.iterator();
for (int[] row : grid) {
for (int i = 0; i < row.length; i++) {
row[i] = iterator.nextInt();
}
}
}

编辑:

如果你想生成一个数独网格即

the same single integer may not appear twice in the same row, column or in any of the nine 3×3 subregions of the 9x9 playing board.

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* @author FaNaJ
*/
public class SudokuGenerator {

private static final int N = 9;
private static final int S_N = 3;

public static int[][] generateSudokuGrid() {
int[][] grid = new int[N][N];
int[] row = {1, 2, 3, 4, 5, 6, 7, 8, 9};

for (int y = 0; y < N; y++) {
int attempts = 0;

do {
if (++attempts > 1000000) { // Oops! I know (sometimes :) it's not a good algorithm...
return generateSudokuGrid();
}
shuffleArray(row);
} while (!isAllowed(grid, y, row));

System.arraycopy(row, 0, grid[y], 0, N);
}

return grid;
}

static boolean isAllowed(int[][] grid, int y, int[] row) {
// check columns
for (int i = 0; i < y; i++) {

for (int j = 0; j < N; j++) {

if (grid[i][j] == row[j]) {
return false;
}

}

}

// check sub grids
int startY = (y / S_N) * S_N;

for (int x = 0; x < N; x++) {
int startX = (x / S_N) * S_N;
for (int j = startX; j < startX + S_N; j++) {
if (j != x) {

for (int i = startY; i < y; i++) {

if (grid[i][j] == row[x]) {
return false;
}

}

}
}
}

return true;
}

static void shuffleArray(int[] array) {
Random random = ThreadLocalRandom.current();
for (int i = N; i > 1; i--) {
swap(array, i - 1, random.nextInt(i));
}
}

static void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}

public static void main(String[] args) {
int[][] grid = generateSudokuGrid();
for (int[] row : grid) {
System.out.println(Arrays.toString(row));
}
}

}

输出:

[3, 4, 6, 9, 1, 2, 7, 8, 5]
[9, 7, 2, 3, 8, 5, 4, 1, 6]
[5, 8, 1, 6, 7, 4, 3, 2, 9]
[7, 6, 3, 8, 2, 9, 1, 5, 4]
[4, 9, 5, 1, 6, 7, 2, 3, 8]
[2, 1, 8, 4, 5, 3, 6, 9, 7]
[6, 2, 4, 5, 9, 1, 8, 7, 3]
[8, 5, 7, 2, 3, 6, 9, 4, 1]
[1, 3, 9, 7, 4, 8, 5, 6, 2]

关于java - 用范围内的随机数字填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36024729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com