随机数独生成 [英] Random sudoku generation

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

问题描述

我正在编写一个应为模拟项目生成随机数独谜题的函数;此函数将要生成的单元格数目作为参数,然后生成单元格索引和要放入这些单元格中的数字. 我在生成单元格索引时遇到问题,我不是编程专家,我找不到一个好的例程来生成索引,并且无法检查出两次或两次以上都不是同一个索引.功能是:

I'm writing a function that should generate random sudoku puzzles for a simulation project; this funcion takes as argoument the number of cells to generate then it generates the indexes of cells and numbers to put in those cells. I have a problem in generation of cells indexes, I'm not an expert in programming and i can't find a good routine to generate indexes and to check out not to be the same index couple two times or more. The funcion is:

void gen_puzzle(int quanti)
{
    if(quanti>81) exit(1);
    indexes* ij=new indexes[quanti];
    int f,g,k, controllo=1;

    do
    {
    for(f=0; f<9; f++)
     for(g=0; g<9; g++)
     {
     puzzle[f][g].num=0;//puts 0 in the sudoku puzzle
     puzzle[f][g].p=0;
     }

//////////////section to improve
out:
    srand(int(time(0)+clock()));

    for(k=0; k<quanti; k++)
     ij[k].i=casuale()-1, ij[k].j=casuale()-1;//generates random indexes of sudoku cells where put random nubers

    for(f=0; f<quanti; f++)
     for(g=f+1; g<quanti; g++)
    {
     if(ij[f].i==ij[g].i && (ij[f].j==ij[g].j)) goto out;

    }
////////////////////

    for(k=0; k<quanti; k++)
     puzzle[ij[k].i][ij[k].j] . num=casuale();//puts random numbers in cells
    }
    while(dataNotGood()); //till sudoku isn't good
}

我要求帮助函数将随机索引放置在ij[]数组中的部分,我使用了goto,但这不是一个好的解决方案,并且如果quanti大于约17则效果不佳. br> casuale()只会返回1到9之间的一个随机数.

I ask help for section where the funcion puts random indexes in ij[] array, i used a goto but it's not a good solution and it doesn't work well if quanti is bigger then 17 about.
casuale() just returns a random number between 1 and 9.

推荐答案

首先,我将剔除所有#pragma omp parallel指令,直到您可以使用的代码为止.现在,它只会降低可读性.

First of all, I'd strip out all the #pragma omp parallel directives until you have code that works. Right now it just reduces readability.

第二,如果要生成未解决"的数独(即,大多数数字未填写的数独),通常要做的是随机填写几个数字,然后通过让计算机解决数独的方法来测试数独.如果计算机成功,则意味着您以适当的数独开始. 在这里,您会找到解决数独​​的算法的很好解释.

Second, if you want to generate "unsolved" sudoku's (i.e. those where most numbers are not filled in) what you usually do is you fill in a few numbers at random and you test the sudoku by letting the computer solve it. If the comupter succeeds it means you started with a proper sudoku. Here you find a nice explanation of an algorithm that solves sudokus.

第三,请注意,您要输入数独谜题的数字受到很大限制.如果3x3块(或9x1的行或列)包含1,则不能向该块(行,列)添加另外的1,如果9x9块包含9个1,则不能为其添加其他1,依此类推因此,最好用可以添加的所有数字填充数组的数组(对于所有3x3块,九个1-9的数组),然后从这些数组中随机取出元素,并将其放入对应的3x3块中的拼图中.这样,您至少可以避免在同一3x3块中添加重复数字的情况.

Third, be aware that the numbers you want to put in a sudoku puzzle are constrained quite a bit. If a 3x3 block (or a 9x1 line or column) contains a 1, you can't add additional 1's to the block (line, column), if a 9x9 block contains nine 1's you can't add additional 1's to it, etc. So probably it is better to fill an array of arrays with all the numbers you can add (nine arrays of 1-9 for all 3x3 blocks) and randomly take elements out of these arrays and put them into the puzzle in the corresponding 3x3 block. This way you at least avoid the situation where you add duplicate numbers to the same 3x3 block.

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

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