数独 - 区域测试 [英] Sudoku - Region testing
问题描述
我正在创建一个数独生成器,使用蛮力"随机方法.我已经能够使用代码检查 x/y 轴是否有重复的数字:
I'm creating a sudoku generator, using a 'brute-force' randomity approach. I have been able to check the x / y axis for duplicate numbers just fine using the code:
for(l=0; l<9; l++){//Makes all vertical work.
if(sudoku[l][j] == temp){
isUsed=true;
}
}
for(m=0; m<9; m++){//makes all horizontal work
if(sudoku[i][m] == temp){
isUsed=true;
}
}
我决定实施框"或区域"检查(从原点开始检查每 3x3 个正方形),但我似乎无法理解代码.这是我到目前为止所做的.我只是无法弄清楚我的逻辑错误在哪里(为了记录,程序将使用此代码运行,但不会正确检查区域)
I decided to implement the 'box' or 'region' checking (where you check every 3x3 square from the origin) and I just can't seem to wrap my head around the code. Here's what I've done so far. I just can't quite figure out where my logic error lies (for the record the program will run with this code, but will not check regions properly)
rowbase = i-(i%3);
if(i==2 || i==5 || i==8 ){
if(rowbase == 0 || rowbase == 3 || rowbase == 6){
isUsed= RegionCheck.RegCheck(rowbase, sudoku);
}
}
regionCheck.java 的内容:
Contents of regionCheck.java:
boolean okay = false;
int[] regionUsed = new int[9];
int i=0, j=0, regionTester=0, counter=0, numcount;
for (i=regionTester; i<regionTester+3; i++){
for (; j<3; j++){
regionUsed[counter]=sudoku[i][j];
counter++;
}
}
for(i=0; i<9; i++){
numcount=regionUsed[i];
for(j=0; j<9; j++){
if(j==i){
//null
}
else if(numcount == regionUsed[j]){
okay=false;
}
}
}
return okay;
一路上我迷路了,不明白如何选择"一个区域并遍历区域.
Somewhere along the way I'm just getting lost and not understanding how to 'select' a region and iterate through regions.
关于如何选择"一个区域进行测试然后对其进行迭代的任何帮助将不胜感激,因为我真的没有想法.
Any help on simply how to 'select' a region for testing and then iterate through it would be greatly appreciated as I'm really out of ideas.
推荐答案
我不明白你的意思测试一个区域,我假设测试意味着每个区域都有从 1 到 9 的每个数字,没有重复.
I cannot understand what you mean testing a region, I assume test means each region has every number from 1 to 9 without duplication.
我们可以实现可能:
- 对垂直、水平和 3x3 区域使用 for 循环.
- 使用每个区域都有位置的表格,包括水平或垂直区域.
我的推荐是第二个.如果您有一个表,则访问区域的循环将执行一次.另一方面,第一个需要为垂直/水平/3x3 实现 3 个类似的循环.
My recommend is 2nd one. If you have a table, the loop which access regions is implemented once. On the other hand, 1st one requires to implement 3 similar loops for vertical/horizontal/3x3.
这是生成表格的代码:
for(int i=0, k=0; i<9; i++) {
// generate vertical regions
for(int j=0; j<9; j++)
table[k][j] = new table_t(i, j);
k++;
// generate horizontal regions
for(int j=0; j<9; j++)
table[k][j] = new table_t(j, i);
k++;
// generate 3x3 regions
for(int j=0; j<9; j++)
table[k][j] = new table_t((i/3)*3+j/3, (i%3)*3+j%3);
k++;
}
生成垂直或水平区域的代码很容易阅读.虽然应该描述 3x3 区域生成.变量i
取0到8,((i/3)*3
,(i%3)*3
)指向每个 3x3 区域的角落.并且 (+j/3
, +j%3
) 移动区域中的每个框.
The code to generate vertical or horizontal regions is easy to read.
Though 3x3 region generation should be described.
The variable i
is taken from 0 to 8, ((i/3)*3
, (i%3)*3
) points the corner of a 3x3 region each. And (+j/3
, +j%3
) moves each box in the region.
您可以通过以下代码测试矩阵sudoku
是否符合:
And you can test the matrix sudoku
comply or not by following code:
boolean okey = true;
for(int i=0; i<27; i++) {
int [] counter = new int[10];
for(int j=0; i<10; i++)
counter[i]=0;
for(int j=0; j<9; j++)
counter[ sudoku[table[i][j].x][table[i][j].y] ] ++;
boolean ok = true;
for(int j=0; j<9; j++)
if(counter[j+1]!=1)
ok = false;
if(!ok)
okey = false;
}
数组counter
统计每个数字出现的次数(我假设 0
是一些特殊的含义,1
和 9
之间的数字是有效的).
The array counter
counts number of appearance for each digit
(I assume 0
is some special meaning and digits between 1
and 9
is valid).
这篇关于数独 - 区域测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!