java中的数独生成器 [英] Sudoku Generator in java

查看:74
本文介绍了java中的数独生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请帮我在java中生成不同顺序的数据,如3x3,4x4,5x5 ......



数独遵循三条规则

1.数字每行只能出现一次

2.每列只能出现一次数字

3。数字只能在每个网格上出现一次



我浏览了数独生成器代码,我得到了下面提到的代码。这个代码能够检查前两个规则,而不是第三条规则。请帮我实施第三条规则。



import java.util。*;



//生成数独网格

公共类SudokuGenerator {



//带有数字的网格

private int [] [] grid;

//单行

private int []行;

//随机和aray列表

private随机运行;

private ArrayList< integer> al;

//行数和列数

private int size;



//大小数独网格作为参数收到

SudokuGenerator(int size){

this.size = size;

//这就是我们的网格将填写

grid = new int [size] [size];

//这是存储我们正在尝试构建的行

row = new int [size];

//随机数生成器

ran = new Random();

// arraylist将会包含网格中每个案例的可能值

al = new ArrayList< integer>();



//现在让''逐行填充网格

for(int i = 0; i< size; i ++)

fillRow(i);

}



//将调用genRow()填充行

//然后将该行复制到网格中...这种方法的原因是显示调试信息

private void fillRow(int n){

genRow(n); //得到一个新行

//好的我可以将行复制到网格中

//System.out.print(\"Row+ n +: );

for(int i = 0; i< size; i ++){

grid [n] [i] = row [i];

//System.out.print(+ row [i]); //可选的调试语句

}

//System.out.println();



}

//用新行填充实例变量行

private void genRow(int n){

//将用于标记哪一行值是否可用

boolean [] used = new boolean [size];

//可能需要多次试用才能填一行

//想象以下案例

// 1 2 3 4

// 2 3 4 1

//现在如果我添加第三行

// 3然后

// 3 4然后

// 3 4 2然后

//最后一列的唯一可能情况是1,但最后一列中的1无效

//所以我们将不得不再试一次

boolean conflict = true; //假设我们有冲突进入while循环

while(冲突){

conflict = false; //假设它有效

//为行的每一行循环

for(int i = 0; i< size; i ++){

//初始化所有数字形式0到大小都不使用且可用

for(int j = 0; j< size; j ++)

used used [j] = false;

//我不能使用该行上使用的任何先前的数字,所以我设置为true

//该行中已分配的所有数字

for(int j = 0; j< i; j ++)

使用[row [j]] = true;

// i不能同时使用已填充的前一行中同一列中使用的数字

for(int j = 0; j< n; j ++)

used [grid [ j] [i]] = true;

//用可能的值填充数组列表

al.clear(); //清空arraylist

for(int j = 0; j< size; j ++){//用允许值填充

if(!used [j ]){//如果没有使用数字

al.add(j); //将它添加到arraylist

}

}

//现在案例解释为变量冲突评论

//在这种情况下,在arraylist中没有输入数字,所以它的大小为0

if(al.size()== 0){

//如果是这样的情况标志存在冲突

conflict = true;

break; //无需继续循环

}

//从arraylist中随机拾取一个数字

row [i] = al.remove (ran.nextInt(al.size()));

}

}

}



//来检索网格

public int [] [] getGrid(){

返回网格;

}

//打印网格(数字从0到1 - )

//但对于普通用户,我们将显示从1到大小

public String toString(){

String str =;

for(int i = 0; i< size; i ++){// for every行

for(int j = 0; j< size; j ++){//和列

str + =+(grid [i] [j] + 1); //为String添加值

}

str + =\ n; //行结束

}

返回str;

}





public static void main(String [] arg){

SudokuGenerator s = new SudokuGenerator(10);

System.out.print( s);

}

}

Hi,
Please help me to generate sudoku of different order like 3x3,4x4,5x5... in java.

Sudoku follows three rules
1. Number can appear only once on each row
2. Number can appear only once on each column
3. Number can appear only once on each grid

I browsed for sudoku generator code and I got the below mentioned code.That code is able to check the first two rules,not the third rule.Please help me in implementing the third rule.

import java.util.*;

// produce a Sudoku grid
public class SudokuGenerator {

// the grid with the numbers
private int[][] grid;
// a single row
private int[] row;
// random and aray list
private Random ran;
private ArrayList<integer> al;
// number of row and column
private int size;

// size of the Sudoku grid is received as parameter
SudokuGenerator(int size) {
this.size = size;
// this this the grid that we will fill
grid = new int[size][size];
// this is to store a row that we are trying to build
row = new int[size];
// random number generator
ran = new Random();
// arraylist that will contain the possible values for every case in the grid
al = new ArrayList<integer>();

// now let''s fill the grid row by row
for(int i = 0; i < size; i++)
fillRow(i);
}

// will call genRow() to fill row
// then will copy that row into the grid... the main reason for this method is to display debug info
private void fillRow(int n) {
genRow(n); // get a new row
// ok i can copy the row into the grid
//System.out.print("Row " + n + ":");
for(int i = 0; i < size; i++) {
grid[n][i] = row[i];
//System.out.print(" " + row[i]); // optional debug statement
}
//System.out.println();

}
// fill the instance variable row with a new row
private void genRow(int n) {
// will be used to flag which values are available or not
boolean[] used = new boolean[size];
// it might take more than one trial to fill a row
// imagine the following case
// 1 2 3 4
// 2 3 4 1
// now if I add for the third row
// 3 then
// 3 4 then
// 3 4 2 then
// the only possible case for the last column is 1 but 1 is invalid in the last column
// so we''ll have to try again
boolean conflict = true; // assume we have conflict to enter the while loop
while(conflict) {
conflict = false; // assume it worked
// loop for each column of the row
for(int i = 0; i < size; i++) {
// initialized that all number form 0 to size are not used and are available
for(int j = 0; j < size; j++)
used[j] = false;
// i cannot use any previous number used on that row so I set to true
// all the already allocated numbers in that row
for(int j = 0; j < i; j++)
used[row[j]] = true;
// i cannot use neither the numbers used in the same column in the previous rows already filled
for(int j = 0; j < n; j++)
used[grid[j][i]] = true;
// fill the array list with the possible values
al.clear(); // empty the arraylist
for(int j = 0; j < size; j++) { // fill it with the permitted values
if(!used[j]) { // if number not used
al.add(j); // add it to arraylist
}
}
// now case explained in comment for variable conflict
// in that case no number would have been entered in the arraylist so its size would be 0
if(al.size() == 0) {
// if it is the case flag that there is a conflict
conflict = true;
break; // no need to continue the loop
}
// pickup a number randomly from the arraylist
row[i] = al.remove(ran.nextInt(al.size()));
}
}
}

// to retreive the grid
public int[][] getGrid() {
return grid;
}
// to print the grid (numbers are from 0 to size-1)
// but for regular user we will display from 1 to size
public String toString() {
String str = "";
for(int i = 0; i < size; i++) { // for every row
for(int j = 0; j < size; j++) { // and column
str += " " + (grid[i][j] + 1); // add value to String
}
str += "\n"; // end of line
}
return str;
}


public static void main(String[] arg) {
SudokuGenerator s = new SudokuGenerator(10);
System.out.print(s);
}
}

推荐答案

到目前为止你尝试了什么? Q& A 以这种方式工作:你努力(努力)自己编写程序,当你遇到问题时,请在这里提出具体问题。阅读常见问题解答会有所帮助。
What have you tried so far? Q&A works this way: you try (hard) to write yourself the program and when you get stuck ask a specific question here. Reading the FAQ would help.


搜索文章部分会找到一个非常好的样本。
A search of the articles section will find you a very good sample.


这篇关于java中的数独生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆