简单的暴力破解
我需要帮助修复对一组数字 1=9 的非常简单的暴力攻击。我的目标是解决一个数独棋盘,其中一行充满 8 个数字,以便该行只能留下一个数字。 我想到找到这个的唯一方法是为丢失的单元格分配一个数字,然后将其与该行中的每个其他数字进行检查。我非常接近解决方案,但返回值始终为 1。这是我遇到问题的代码片段: 我更新了代码,以便它现在添加所有数字并从 45 中减去它们找到正确的号码。它仍然没有返回正确的数字。它返回 423(其中缺少数字 6)。
int radd1=deduct[0][0]+deduct[0][1]+deduct[0][2]+deduct[0][3]+deduct[0][4]+deduct[0] int test=0;
if (radd1==8) {
for (int control=0; control<9; control++) {
if (dash[0][control]=='_') {
empty=control;
}
}
for (int control2=0; control2<9; control2++) {
if (control2!=empty) {
test=test+dash[0][control2];
}
}
cout << test << endl;
}
更多信息:
整个解决方案基于一个 9x9 字符,其中填充了数字,并将其放置在适当的位置。该字符称为 dash[9][9]
。deduct[9][9]
字符与 dash[9][9]
重复,只不过那里不是实际的数字,而是一个 1
。这样我就可以将整行相加,如果它等于8
,则执行简单的暴力破解(如果这是正确的术语)来找到最后的第九个数字。 empty
是一个 int。它存储(在本例中)没有编号的列号。
发现任何问题吗?
I need help fixing a very simple brute force attack on a set of numbers 1=9. My goal is to solve a sudoko board where a row is filled with 8 numbers so that there can only be one number left on the row. The only way that I thought of to find this was to assign a number to the missing cell and then check it against every other number on the row. I'm really close to the solution but the return is always a 1. Here's the snippet of code that I'm having problems with: I updated the code so that it now adds all the numbers and subtracts them from 45 to find the right number. It still doesn't return the right number. It returns 423 (With 6 as the missing number).
int radd1=deduct[0][0]+deduct[0][1]+deduct[0][2]+deduct[0][3]+deduct[0][4]+deduct[0] int test=0;
if (radd1==8) {
for (int control=0; control<9; control++) {
if (dash[0][control]=='_') {
empty=control;
}
}
for (int control2=0; control2<9; control2++) {
if (control2!=empty) {
test=test+dash[0][control2];
}
}
cout << test << endl;
}
Some more info:
The entire solve is based off of one 9x9 char filled with numbers to go in their appropriate spots. This char is called dash[9][9]
.
The deduct[9][9]
char is a duplicate of dash[9][9]
except that instead of the actual number that goes there, there is a 1
. This is so I can add up a whole row and if it is equal to 8
perform my simple brute force (if that's the right term) to find the final ninth number. empty
is an int. It stores (in this case) the column number that has no number.
Spot any problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
deduct[0][9]
是错误的,对吧?dash[0][control3]
的比较在循环内进行,因此 test 的值始终依赖于循环的最后一次迭代。 (在本例中control2+1
与dash[0][8]
。我想如果存在相等性,您就必须打破循环。control2+'1'
到dash[0][empty]
但后来使用control+1
(不是同一件事)if (test==1)
感觉不对,但我们无法仅从包含的代码中确定它是否正确。有一种更好的方法可以找到尚未使用的数字。我们知道
1+2+.. +9 = 45
,因此您只需将出现的数字相加,然后从 45 中减去即可获得缺少的数字。deduct[0][9]
in the addition of radd1, right?dash[0][control3]
is inside a cycle so that the value of test is always dependant in the last iteration of the cycle. (In this casecontrol2+1
vsdash[0][8]
. I suppose you'd have to break the cycle if there is an equality.control2+'1'
todash[0][empty]
but later usecontrol+1
. (Not the same thing)if (test==1)
doesn't feel right but we can't be sure if it's correct just from the included code.There is a better way of finding the number that has not been used. We know the sum of
1+2+..+9 = 45
, so you'd only have to sum the appearing numbers and substract from 45 to obtain the number that's missing.