這段代碼是為了解決我在舊貨店找到的一個木制拼圖問題而開發的,它有許多刻有數字的木輪。為了這個程式的目的,這些輪子被轉化為一個二維矩陣,并有一些額外的規則來描述其相對運動的界限。
代碼的計算部分正在作業。我從控制臺收到的配置布局在謎題的物理和數學范圍內是有效的。然而,應該只有 16^4 種可能的配置,而在超過 75,000 次迭代之后,每一次迭代都是獨一無二的(就我所能看到的接近一百萬行的輸出而言),仍然沒有有效的解決方案。
我怎么會漏掉一個呢?
底線是什么?
底線,行需要保持相同的順序,但可以移動("旋轉")任何數量的空格,盡管16個空格的移動涵蓋了每個車輪的整個旋轉布局。
一個輪子由兩個同心的徑向數字集組成,有些有空格(空值,但在本代碼中表示為 "N",上面一個輪子上的外側數字集與下面一個輪子上的內側數字集重疊,這樣,第一行和第二行是一個輪子(在數學上保持靜止的那個,因為所有其他輪子可以相對于它移動),第三行和第四行是一個輪子(表盤可能是一個更恰當的術語)。因此,turnDial()函式是同時為兩行數字呼叫的,除了最后一行,它是一個獨立的表盤,只有一組數值,其中一半是空的(表明物理輪子上有一個空位,在它下面的輪子上的數字將是 "data_Output "陣列上的數值,代表了根據工廠配置堆放時表盤的實際物理布局。CheckSum()對數值是來自上面的輪子還是下面的輪子進行了分類,它還為每一列生成了一個和,以與所需的解決方案進行比較(每一列的數字加起來必須是 "50",謎題才能被解開),并輸出這些和的總和,在任何有效的解決方案中,它都等于800(50*16)列,并且可以輕松縮小解決方案是否有效(如果沒有10萬個解決方案需要分類)。
不管怎樣,它運行了。它輸出了資料,我只是沒有看到它輸出有效的解決方案,所以我可能錯過了什么。一籌莫展。希望得到幫助。
控制臺螢屏截圖#include "pch.h"/span>
#include <iostream>
using namespace System;
using namespace std;
using namespace System::IO;
const int depth = 9; // (ROWS)/span>
const int length = 16; // (COLUMNS)
int target_Column_Total = 50;
int The_Grail[length] = { 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50 };
int Sum[16] 。
int sum_Total;
long int Iteration;
int data_Input[depth][length] =
{ {16, 4, 7, 0, 16, 8, 4, 15, 7, 10, 1, 10, 4, 5, 3, 15],
{2, 9, 27, 13, 11, 13, 10, 18, 10, 10, 10, 10, 15, 7, 19, 18},
{ 6, 'N'/span>, 10, 'N'/span>, 8, 'N'/span>, 10, 'N'/span>, 9, 'N'/span>, 8, 'N', 8, 'N', 9, 'N'] 。
{ 5, 1, 24, 8, 10, 20, 7, 20, 12, 1, 10, 12, 22, 0, 5, 8 },
{ 0, 'N'/span>, 11, 'N'/span>, 8, 'N', 8, 'N', 8, 'N'/span>, 10, 'N'/span>, 11, 'N'/span>, 10, 'N'/span> },
{ 20, 8, 19, 10, 15, 20, 12, 20, 13, 13, 0, 22, 19, 10, 0, 5 },
{ 14, 'N'/span>, 11, 'N'/span>, 8, 'N'/span>, 12, 'N', 11, 'N'/span>, 3, 'N'/span>, 8, 'N'/span>, 10, 'N'/span> },
{ 8, 17, 4, 20, 4, 14, 4, 5, 1, 14, 10, 17, 10, 5, 6, 18 },
{ 8, 'N'/span>, 16, 'N'/span>, 19, 'N'/span>, 8, 'N'/span>, 17, 'N'/span>, 6, 'N'/span>, };
int data_Output[5]【length】。
void checkSum()。
void turnDial(int) ;
void main()
{
while (true) {
for (int i = 0; i < length; i ) {
turnDial(2)。
turnDial(3)。
checkSum()。
for (int j = 0; j < length; j ) {
turnDial(4)。
turnDial(5)。
checkSum()。
for (int k = 0; k < length; k ) {
turnDial(6)。
turnDial(7)。
checkSum()。
for (int l = 0; l < length; l ) {
turnDial(8)。
checkSum()。
}
}
}
}
}
}
void checkSum() {
迭代 。
for (int i = 0; i < 5; i ) {
for (int j = 0; j < length; j ) {
if (i == 0) {
data_Output[i][j] = data_Input[i][j]。
}
if (i == 1) {
if (data_Input[2] [j] !='N') {
data_Output[i][j] = data_Input[2] [j];
}
else {
data_Output[i][j] = data_Input[1] [j];
}
}
if (i == 2) {
if (data_Input[4] [j] != 'N') {
data_Output[i][j] = data_Input[4] [j];
}
if (data_Input[4][j] == 'N'/span>) {
data_Output[i][j] = data_Input[3] [j];
}
}
if (i == 3) {
if (data_Input[6] [j] !='N') {
data_Output[i][j] = data_Input[6] [j];
}
if (data_Input[6][j] == 'N'/span>) {
data_Output[i][j] = data_Input[5] [j];
}
}
if (i == 4) {
if (data_Input[8] [j] !='N') {
data_Output[i][j] = data_Input[8] [j];
}
if (data_Input[8][j] == 'N'/span>) {
data_Output[i][j] = data_Input[7] [j];
}
}
}
}
cout << "Data:" << "
"。
for (int x = 0; x < depth; x ) {
for (int y = 0; y < length; y ) {
cout << data_Input[x][y] << " "/span>;
}
cout <<endl;
}
cout << endl;
cout << "Orienation:" << "
"。
for (int x = 0; x < 5; x ) {
for (int y = 0; y < length; y ) {
cout << data_Output[x][y] << " "/span>;
}
cout <<endl;
}
cout << "
"。
for (int y = 0; y < 5; y ) {
for (int x = 0; x < length; x ) {
if (y == 0) {
Sum[x] = 0;
}
Sum[x] = data_Output[y][x]。
if (y ==(4)) {
cout << Sum[x] << " "/span>;
}
}
}
sum_Total = { 0 };
for (int i = 0; i < length; i ) {
sum_Total = Sum[i];
}
cout << " Sum: " << sum_Total;
cout << " #: "/span> << Iteration << "
";
if (Sum != The_Grail) {
cout << "沒有骰子。" << "
"。
}
if (Sum == The_Grail) {
cout << "SUCCESS!" << "
"。
exit(888)。
}
// int Sum[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };.
}
void turnDial(int) {
int holder = data_Input[x][(length - 1)]。
for (int i = (length - 1); i > 0; i--) {
data_Input[x][i] = data_Input[x][i - 1] 。
}
data_Input[x][0] = holder;
}
uj5u.com熱心網友回復:
我還沒有仔細研究過所有的東西,但是最后的比較(Sum == The_Grail)總是會失敗,因為這是在比較兩個陣列指標,而不是(人們可能合理地期望)陣列中的每個值。我將從回圈瀏覽陣列開始,對每個元素進行比較。
此外,你是否確定輸入的資料能夠保證產生一個解決方案?換句話說,輸入的資料是已知的好資料還是隨機的呢?
uj5u.com熱心網友回復:
Pstephan,謝謝你的幫助,在迭代58344上釘住了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/319969.html
標籤:
下一篇:根據參賽作品的數量計算獎牌的數量
