数独编码帮助 [英] sudoku coding help

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

问题描述

hy guys


i我的数独程序遇到问题我用c ++编码。

目前在我的程序中如果存在重复的数字要么

行/列/块我会使特殊的平方0.但这不是我想要做的b $ b。我想要回复,直到它找到一个正确的

数字。我将发布我需要帮助的功能;

---编码--------------------------- -------------------------------

void generation(int newGrid [9] [9] )

{

int randV; //变量

int r,c;

for(r = 0; r< 9; r ++)

{

for(c = 0; c <9; c ++)

{

randV =(rand()%9)+1; //随机生成器

if(newGrid [r] [c] == 0)//如果空方格

{

newGrid [r ] [c] = randV;

if(!isLegal())//检查行/列/网格

newGrid [r] [c] = 0; //使广场0

}

}

}

hy guys

i am having a problem with my sudoku program which i coded using c++.;
currently in my program if a duplicate number exist in either
row/column/block i would make the particualr square 0. but thats not i
want to do. I want to recurse back until until it find a correct
number. i will post the function which i need the help;
---coding----------------------------------------------------------
void generation(int newGrid[9][9])
{
int randV; //variables
int r,c;
for( r=0;r<9;r++)
{
for(c=0;c<9;c++)
{
randV=(rand()%9)+1; //random generator
if(newGrid[r][c]==0) //if empty squares
{
newGrid[r][c]=randV;
if(!isLegal()) //check row/column/grid
newGrid[r][c]=0; //make the square 0
}
}
}

推荐答案

ka*********@gmail.com
hy guys


Hy自己

i遇到了我使用c ++编码的数独程序的问题。 />目前在我的程序中,如果
行/列/块中存在重复的数字,我会将其设为squarealr square 0.但这不是我想要做的。我想要回复,直到找到正确的数字。


什么是recurse back?回到哪里?


" Recurse"通常意味着呼唤自己。或通过

调用其他函数,最终调用此函数来调用自身。什么意思

这样做回来?您的意思是,或许,返回某种错误代码,以便

您的函数的调用者会再次调用它吗?


也许如果您描述,一种人类语言,最好是英语,你希望你的程序能够做什么,我们可以尝试帮助你把它放在

C ++术语中......

i将发布我需要帮助的功能;

---编码------------------------ ----------------------------------
void generation(int newGrid [9] [9])
{
int randV; //变量
int r,c;

for(r = 0; r< 9; r ++)
{
for(c = 0; c< 9 ; c ++)
{
randV =(rand()%9)+1; //随机生成器

if(newGrid [r] [c] == 0)//如果是空方格
{

newGrid [r] [c] = randV;

if(!isLegal())//检查row / column / grid
newGrid [r] [c] = 0; //使广场0

}

}
}
hy guys
Hy yourself
i am having a problem with my sudoku program which i coded using c++.;
currently in my program if a duplicate number exist in either
row/column/block i would make the particualr square 0. but thats not i
want to do. I want to recurse back until until it find a correct
number.
What is "recurse back"? Back where?

"Recurse" usually means "call itself" or "cause a call to itself through
calling other function that eventually calls this one". What does it mean
to do it "back"? Did you mean, maybe, return some kind of error code so
that the caller of your function would call it again?

Perhaps if you describe, in a human language, preferably English, what
you expect your program to do, and we could try helping you to put it in
C++ terms...
i will post the function which i need the help;
---coding----------------------------------------------------------
void generation(int newGrid[9][9])
{
int randV; //variables
int r,c;
for( r=0;r<9;r++)
{
for(c=0;c<9;c++)
{
randV=(rand()%9)+1; //random generator
if(newGrid[r][c]==0) //if empty squares
{
newGrid[r][c]=randV;
if(!isLegal()) //check row/column/grid
newGrid[r][c]=0; //make the square 0
}
}
}




V

-

请在邮寄回复时从我的地址中删除资金



V
--
Please remove capital As from my address when replying by mail


< ka ******* **@gmail.com>写道:
<ka*********@gmail.com> wrote:
我的数独程序使用c ++进行编码时出现问题。;
目前在我的程序中,如果行中存在重复的数字
行/ column / block我会使particalr平方0.但那不是我想要做的。我想要回复,直到找到正确的数字。我将发布我需要帮助的功能;

---编码--------------------------- -------------------------------
void generation(int newGrid [9] [9])
{
int randV; //变量
int r,c;

for(r = 0; r< 9; r ++)
{
for(c = 0; c< 9 ; c ++)
{
randV =(rand()%9)+1; //随机生成器

if(newGrid [r] [c] == 0)//如果是空方格
{

newGrid [r] [c] = randV;

if(!isLegal())//检查row / column / grid
newGrid [r] [c] = 0; //使广场0

}

}
}
i am having a problem with my sudoku program which i coded using c++.;
currently in my program if a duplicate number exist in either
row/column/block i would make the particualr square 0. but thats not i
want to do. I want to recurse back until until it find a correct
number. i will post the function which i need the help;
---coding----------------------------------------------------------
void generation(int newGrid[9][9])
{
int randV; //variables
int r,c;
for( r=0;r<9;r++)
{
for(c=0;c<9;c++)
{
randV=(rand()%9)+1; //random generator
if(newGrid[r][c]==0) //if empty squares
{
newGrid[r][c]=randV;
if(!isLegal()) //check row/column/grid
newGrid[r][c]=0; //make the square 0
}
}
}




生成它的意图一个完整的数组,删除一些数字,并将残差显示为数独。是对的吗?还有一个先决条件,

newgrid [9] [9]在调用生成时包含全零,是吗?

如果我的猜测是对的,那么你必须摆脱前提条件递归调用

代。


我没有看到任何使用递归的理由,除非这是一个学校作业。

我建议你学习shuffle。并重新开始。使用这部分

代码。我认为,到目前为止,你的注定速度非常慢。没有

shuffle会让它变得简单,但它看起来更合理

起点 - 但可能还是显得慢得多......


我认为你已经开始编码而没有充分注意设计。

试试这个:当检测到问题时打印r和c的最高值。

每次实现新记录时都打印一条消息。我认为r将会羞于9,时间 - 挂钟时间 - 在新的高点之间是

将在几分钟内完成测量。 />



The intent it to generate a complete array, remove some digits, and present
the residue as a sudoku. Is that right? And there is a pre-condition,
newgrid[9][9] contains all zeros when generation is called, is that right?
If my guess is right, you would have to get rid of the pre-condition to call
generation recursively.

I don''t see any reason to use recursion unless this is a school assignment.
I suggest you learn about "shuffle" and start over. with this part of your
code. What you have so far is, I think, doomed to be incredibly slow. Not
that shuffle will make it a cinch, but it seems like a more reasonable
starting point - but possibly still unusably slow..

I think you have started coding without sufficient attention to the design.
Try this: print the highest value of r and c when a problem is detected.
Print a message every time a new record is achieved. I think r is going
to be way shy of 9 and the time - wall clock time - between new highs is
going to already be measured in many minutes.


ka*********@gmail.com 写道:
hy guys

我的数独程序使用c ++进行编码有问题。;
目前在我的程序中
row / column / block中存在重复的数字我会使particalr平方为0.但那不是我想要做的。我想要回复,直到找到正确的数字。我将发布我需要帮助的功能;
hy guys

i am having a problem with my sudoku program which i coded using c++.;
currently in my program if a duplicate number exist in either
row/column/block i would make the particualr square 0. but thats not i
want to do. I want to recurse back until until it find a correct
number. i will post the function which i need the help;




我假设你想要在非法情况下回溯。这个

确实可以通过递归来完成最简单的(未经测试):


bool

generation(int newGrid [9 ] [9],int r,int c)

{

if(c == 9)

{

if(++ r == 9)

返回true;

c = 0;

}



{

const int randV =(rand()%9)+ 1;

newGrid [r] [c] = randV;

if(!isLegal(newGrid,r,c))

返回false;

}

while( !代(newGrid,r,c + 1));

返回true;

}


您最初会调用它r和c = 0。


这就是说我很确定它永远不会飞,因为它太慢了。

如果有足够的时间它应该在principal创建一个有效的sudoku

(一个糟糕的rand()实现可以在无限循环中发送它。)



I assume you want to backtrack if you reach an illegal situation. This
can indeed be done easiest with recursion like so (not tested):

bool
generation(int newGrid[9][9], int r, int c)
{
if(c == 9)
{
if(++r == 9)
return true;
c = 0;
}
do
{
const int randV = (rand() % 9) + 1;
newGrid[r][c] = randV;
if(!isLegal(newGrid, r, c))
return false;
}
while(!generation(newGrid, r, c + 1));
return true;
}

You would initially call it with r and c = 0.

That said I am pretty sure this will never fly because it is too slow.
Given enough time it should however in principal create a valid sudoku
(a poor rand() implementation could send it in an infinite loop though).


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

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